ASP.NET MVC Session.IsNewSession issue with Google Chrome

I am currently working on the completed part of the logical loop for my ASP.NET 3.5 MVC 2 project to log out and redirect them to the LogOn AccountController action.

I have the following attribute for all my actions that relate to session state, and this piece of code works in IE 8 but not Firefox 4 or Google Chrome 10. The symptom is that I am trying to go to the view represented by the action with my attribute [SessionExpireFilter], the ctx.Session.IsNewSession property in the code below evaluates to “true” every time, even if I'm only seconds in my 30 minute session.

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); } } 

Is there a way to find out why Chrome and Firefox behave this way, but IE not? Thanks in advance.

EDIT: This does not work in FF, as I expected. I head to my LogOn action immediately after logging in and trying to access the action using the SessionExpireFilter attribute.

+8
c # firefox google-chrome asp.net-mvc session-timeout
source share
2 answers

ASP.NET will create a new session for each request if you have not saved something in it. Try adding the code below to Global.asax . It works in my MVC2 and MVC3 applications with the same SessionExpireFilterAttribute .

 protected void Session_Start() { Session["Dummy"] = 1; } 
+14
source share

We can add the session_start method to the Golbal.asax file in the MVC application.

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

Then, when the application that starts your session is created. and then the session will not be isNewSession "True" , otherwise it will always be "True"

+1
source share

All Articles