0

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 in my webconfig file. 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");
        }

 }

1 Answer 1

1

There is no way to force on Session key to expire. For this scenario you have to use Cache.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Vijay any idea how to implement it using cache. I am new to session. Thanks
string userIdentifier = string.Empty; Cache.Add("Key" + userIdentifier, "Value 1", null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
Cache is shared among users, user Identifier here is to identity current user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.