Here is the scenario when user logged in to our website and keeps it on then after certain period of time when session gets expired then it should automatically redirect to login page.
protected void Session_Start(object sender, EventArgs e)
{
// Check user session
if (Session["userSession"] != null)
{
//Redirect to Home Page if Session is
not null
Response.Redirect("Index.aspx");
}
else
{
//Redirect to Login Page if Session is
null & Expires
Response.Redirect("Login.aspx");
}
}
protected void Session_End(Object sender, EventArgs e) {
Session.Clear();
Session.Abandon();
}
Step 2: Make sure you have below code in your web.config
file
<sessionState timeout="5"></sessionState> <!—Session
time out in minutes-->
Step 3: Now, add below code in your page (in master or any
page) in .aspx.cs on page load
Response.AppendHeader("Refresh", Convert.ToString((Session.Timeout)
* 60) + "; URL=Login.aspx");
Here Session.Timeout is your web.config time out
Now, you are done. When user session time out then it
automatically redirect to home page.
Comments