2012-12-07

Redirect the user to login page when session timeouts

If the webpage’s session expirers due to the idle state, you may need to redirect the user to login page/default page to prevent unexpected errors/results. Easiest way to handle this is on the Session_Start event of the global.asax file. Whenever a new session starts, the Session_Start event get fired. Whenever the current session timeouts, a new session will get started. In other words, Session_Start even in the global.asax file will get fires whenever the session expires. So add the below code in global.asax file

void Session_Start(object sender, EventArgs e)
{
     // Code that runs when a new session is started
     Response.Redirect("~/Login.aspx");
}

The above code will redirect the user to Login page whenever a session timeouts. So you don’t have to worry about checking whether a session is null or not before proceed.