在部署 asp.net mvc 3 项目时,我最近遇到了这个问题,我不知道它为什么会发生。
我有一个名为 LoginSesion 的类,当用户登录并存储在会话中时,它将获取经过身份验证的用户。
public static LoginSession AuthenticatedUser
{
get
{
if (HttpContext.Current.Session["LoginSession"] != null)
return HttpContext.Current.Session["LoginSession"] as LoginSession;
return null;
}
set
{
HttpContext.Current.Session["LoginSession"] = value;
}
}
当我运行项目时,尝试将用户重定向到特定的 URL(例如http://localhost/user/details/1
), HttpContext.Current.Session["LoginSession"] 变为 Null 并将用户重定向回登录页面。
奇怪的是,这并不总是无效的,只是有时。当服务器运行太慢时,虽然会话尚未过期,但也会发生这种情况。
我在 web.config 中设置了会话超时,如下所示:
<authentication mode="Forms">
<forms loginUrl="~/UserProfiles/Logon" timeout="2880" />
</authentication>
和
<sessionState mode="InProc" timeout="2880" />
我正在使用 IIS 7.x 进行发布和测试。