alan
alan

Reputation: 6943

ASP.NET MVC Web App - Session Randomly Fails

I've a Web App that just recently has began randomly losing sessions. The exact cause is elusive at best, however it seems the session is killed/lost on the server side and results in the user needing to close their browser entirely and relaunch in order to log back in.

I wish I could provide some code, but I can't figure out where the problem is at all.

Here is a session action filter we use currently:

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

        //if(

        // check if session is supported   
        if (lvContext.Session != null)
        {

            // check if a new session id was generated   
            if (lvContext.Session.IsNewSession)
            {

                // If it says it is a new session, but an existing cookie exists, then it must   
                // have timed out   
                string sessionCookie = lvContext.Request.Headers["Cookie"];
                if ((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0))
                {

                    lvContext.Response.Redirect("~/Account/Timeout");
                }
            }
        }


        base.OnActionExecuting(filterContext);
    }
}

Upvotes: 0

Views: 519

Answers (2)

alan
alan

Reputation: 6943

Ultimately I moved to SQL State Server to handle my sessions. This outsources session handling to the SQL server allowing a session to persist through a recycle, etc. For more information see these links:

Upvotes: 0

Cristi Todoran
Cristi Todoran

Reputation: 519

Did you add a new feature that adds or removes files from the root directory or any of its subdirectories? That can cause the session to reset.

Upvotes: 1

Related Questions