Handling expired sessions in ASP.NET

by Kevin Goff 13. March 2008 00:17

HOW TO: Send a user to a timeout page when their session has expired using ASP.NET 2.0

ASP.NET

Are your users getting error messages when their session times out? You can easily forward your users on to a page letting them know that their session has expired and they need to begin again. The code below handles this rather gracefully:

    1     void Session_Start(object sender, EventArgs e)

    2     {

    3         // check the header for the presence of the cookie -- if it exists then it is an expired session

    4         string _cookieHeader = System.Web.HttpContext.Current.Request.Headers["Cookie"];

    5         if ((null != _cookieHeader) && (_cookieHeader.IndexOf("ASP.NET_SessionId") >= 0))

    6         {

    7             System.Web.HttpContext.Current.Response.Redirect("/expiredSession.htm");

    8         }

    9     }

Tags: , ,