c# - 如何在 OWIN ASP.NET MVC5 中注销用户

标签 c# session asp.net-mvc-5 logout owin

我有一个 ASP.NET MVC5 项目的标准 AccountController 类。 当我尝试注销用户时遇到错误,因为 HttpContextnull。 (我的意思是这里 HttpContext.GetOwinContext().Authentication 是 null)

所以我不知道我们如何在 session 结束时注销用户...

global.asax 我有这个

protected void Session_Start(object sender, EventArgs e)
{
     Session.Timeout = 3; 
}

protected void Session_End(object sender, EventArgs e)
{
            try
            {
                 var accountController = new AccountController();
                 accountController.SignOut();
            }
            catch (Exception)
            {
            }
}

账户 Controller

public void SignOut()
{
      // Even if I do It does not help coz HttpContext is NULL
      _authnManager = HttpContext.GetOwinContext().Authentication;    

    AuthenticationManager.SignOut();


}

private IAuthenticationManager _authnManager;  // Add this private variable


public IAuthenticationManager AuthenticationManager // Modified this from private to public and add the setter
{
            get
            {
                if (_authnManager == null)
                    _authnManager = HttpContext.GetOwinContext().Authentication;
                return _authnManager;
            }
            set { _authnManager = value; }
}

Startup.Auth.cs

 public void ConfigureAuth(IAppBuilder app)
        {
            // Enable the application to use a cookie to store information for the signed in user
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                ExpireTimeSpan = TimeSpan.FromMinutes(3),
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login")
            });
}

最佳答案

假设您正在使用 ApplicationCookie 来存储您的登录信息。

AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);

关于c# - 如何在 OWIN ASP.NET MVC5 中注销用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26182660/

相关文章:

node.js - PassportJS session 混淆了

Global.asax 中的 Asp.net System.Web.HttpContext.Current.Session null

java - Spring session 中带有 HeaderHttpSessionStrategy 的 Null HttpSessionManager

c# - sql 'timestamp' 列真的是 'byte[]' 吗?

c# - linkbutton 没有在 asp.net 中回发

c# - 给FastReport绑定(bind)一个LINQ查询,只得到一行数据

c# - 从 List<List<String>> 中查找不同的元素

c# - MVC 5 绕过 Windows 身份验证用户的表单例份验证

c# - 如何对 try 主体中具有系统调用的异常处理程序进行单元测试?

c# - 如何获得图像的分辨率? (JPEG、GIF、PNG、JPG)