我遇到了 ASP.NET MVC 的问题,它强制用户在大约 20 分钟不活动后重新登录。
我正在使用表单身份验证,并在配置文件中增加了超时:
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="9999999" />
</authentication>
我还在配置文件中将会话超时设置为:
<sessionState timeout="120"></sessionState>
我基于 Rockford Lhotka 的 CSLA ASP.NET MVC 示例,并在我的 global.asax 中包含以下内容:
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
if (HttpContext.Current.Handler is IRequiresSessionState)
{
if (Csla.ApplicationContext.AuthenticationType == "Windows")
return;
System.Security.Principal.IPrincipal principal;
try
{
principal = (System.Security.Principal.IPrincipal)
HttpContext.Current.Session[MyMembershipProvider.SESSION_KEY];
}
catch
{
principal = null;
}
if (principal == null)
{
if (this.User.Identity.IsAuthenticated && this.User.Identity is FormsIdentity)
{
// no principal in session, but ASP.NET token
// still valid - so sign out ASP.NET
FormsAuthentication.SignOut();
this.Response.Redirect(this.Request.Url.PathAndQuery);
}
// didn't get a principal from Session, so
// set it to an unauthenticted PTPrincipal
BusinessPrincipal.Logout();
}
else
{
// use the principal from Session
Csla.ApplicationContext.User = principal;
}
}
}
据我所知,它应该只在 120 分钟不活动后超时……但由于某种原因,它似乎总是在 20 分钟不活动后超时。我知道为什么会发生这种情况,有什么想法吗?
我正在考虑仅转储表单身份验证并通过会话自己处理它,但我担心我会失去 [Authorize] 属性等功能。尽量不要走这条路。
是否可以将我的自定义主体对象存储为 cookie?我只是不想为每个页面或操作都验证/授权用户。
我正在快速脱发! =)