【发布时间】:2014-11-28 17:32:54
【问题描述】:
我有一个 ASP.NET MVC5 项目的标准 AccountController 类。
当我尝试注销用户时,我遇到了一个错误,因为 HttpContext 是 null。 (我的意思是HttpContext.GetOwinContext().Authentication 为空)
所以我不知道如何在会话结束时注销用户...
在 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)
{
}
}
帐户控制器
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")
});
}
【问题讨论】:
标签: c# session asp.net-mvc-5 logout owin