Google Chrome的ASP.NET MVC Session.IsNewSession问题

Pan*_*ait 8 c# asp.net-mvc firefox google-chrome session-timeout

我正在为我的ASP.NET 3.5 MVC 2项目编写一个Session过期的逻辑片段来注销用户并将它们重定向到AccountController LogOn操作.

我对所有关心会话状态的操作都有以下属性,这段代码适用于IE 8,但不适用于Firefox 4或Google Chrome 10.症状是当我尝试导航到由操作表示的视图时我的[SessionExpireFilter]属性,下面代码中的ctx.Session.IsNewSession属性每次都评估为"true",即使我在30分钟的会话中只有几秒钟.

public class SessionExpireFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpContext ctx = HttpContext.Current;

        // check if session is supported 
        if (ctx.Session != null && ctx.Session.IsNewSession)
        {
            // If it says it is a new session, but an existing cookie exists, then it must 
            // have timed out 
            string sessionCookie = ctx.Request.Headers["Cookie"];
            if ((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0))
            {
                FormsAuthentication.SignOut();
                ctx.Response.Redirect("~/Account/LogOn");
            }
        }

        base.OnActionExecuting(filterContext);
    }
} 
Run Code Online (Sandbox Code Playgroud)

有没有办法弄清楚为什么Chrome和Firefox的表现如此,但IE不是?提前致谢.

编辑:这不像我原先认为的那样在FF中工作.登录并尝试使用我的SessionExpireFilter属性访问操作后,我立即被定向到我的LogOn操作.

Jak*_*cki 14

ASP.NET将为每个请求创建一个新会话,除非您在其中存储内容.尝试将以下代码添加到您的Global.asax.它适用于我的MVC2和MVC3应用程序SessionExpireFilterAttribute.

protected void Session_Start()
{
    Session["Dummy"] = 1;
}
Run Code Online (Sandbox Code Playgroud)