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

我正在为我的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); } } 

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

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

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

 protected void Session_Start() { Session["Dummy"] = 1; } 

我们可以在MVC appliaction中的Golbal.asax文件中添加session_start方法。

  protected void Session_Start(object sender, EventArgs e) { HttpContext.Current.Session.Add("UserId" , null); } 

然后,将创建启动会话的应用程序。 然后会话将不是isNewSession’True ,否则它将永远是‘True’