Eric Mbiada

Eric Mbiada

  • 2k
  • 70
  • 18.6k

Manage Session Timeout using ASP.net

Jun 20 2017 8:32 AM
Hello

I have two sessions in my code:

Session["Email"] for user Login Authentication

Session["prevUrl"] to remember last page visited

I want to know how to manage the session timeout . I only want the Session["Email"] to timeout so the user can be redirected to the last page visited when they log back in. At the moment both sessions are timing out after 1 min

Thanks

Web.config

 <sessionState mode="InProc" customProvider="DefaultSessionProvider" timeout="1">  
</sessionState>

Global.asax.cs

void Session_Start(object sender, EventArgs e) 
{

// Code that runs when a new session is started
if (Session["prevUrl"] != null)
{

Response
.Redirect((string)Session["prevUrl"]); //Will redirect to previous page visited
 
}
else

{
//Redirect to Login Page if Session is null & Expires
Response.Redirect("Login.aspx");
}
}

C# Page Load

 protected void Page_Load(object sender, EventArgs e) 
{
Session
["prevUrl"] = Request.Url;
 if (Session["Email"] == null)
{
Response.Redirect("Login.aspx");
}
}
 

Answers (5)