我有一个 ASP.NET MVC5 项目的标准 AccountController 类。
当我尝试注销用户时遇到错误,因为 HttpContext
为 null
。 (我的意思是这里 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/