Once I have created a session variable, can I use it in every way on asp.net?

Viewed 84
Session["USER_ID"]  = "1";

This is the session variable. I want to know how long the session variable value can be stored.

1 Answers

Session duration in ASP.NET is 20 minutes. There are two ways to change the session duration:

  • Setting a value to the timeout attribute in the <sessionState> field in the web.config file.
  • To set Session.Timeout property in Session_Start() method in Global.asax.cs file.

You can change the session duration in the web.config file as follows:

<configuration>
    <system.web>
        <sessionState mode="InProc" cookieless="false" timeout="60" />
    </system.web>
</configuration>

You can change the session duration in the global.asax.cs file as follows:

public class ApplicationName : System.Web.HttpApplication
{
    protected void Session_Start(object sender, EventArgs e)
    {
        /* Sets the session duration to 60 minutes. */
        Session.Timeout = 60;
    }
}

References
Related