简体   繁体   中英

set the timeout for a specific session

How I can set the timeout for a session specific in ASP.NET? it is possible? I have two or more sesions in my web application, I need set:

Session["foo"] //expire in 14minutes
Session["baa"] //expire in 30minutes

I'm wanting solve this case,if possible not using cookies.

Thanks in advance!

You can configure the timeout for a session in the Web.config file:

<configuration>
    <sessionstate mode="inproc"
                  cookieless="true"
                  timeout="14" />
</configuration>

This is the default mode (InProc) for ASP.NET. As rockinthesixtring said in his comment, you can't configure the timeout for individual objects in the session, just the session as a whole.

ASP.NET Session State Overview

Since other questions are being closed as duplicates of this question I would like to add this option here for people who may find it useful.

You can set the Session Timeout manually using the following:

Session.Timeout = 60

You can read more here:

https://msdn.microsoft.com/en-us/library/ms525473(v=vs.90).aspx

for specific session lets say you want to make timeout for this session Session["foo"] =14 min you can do like this

DateTime timenow=DateTime.now;
DateTime timeafter14min=timenow.AddMinuits(14);

if(DateTime.Now>timeafter14min)
{
 HttpContext.Current.Session["foo"]=null;
}

session not expired but cleared

    <sessionState 
       mode="InProc" 
       stateNetworkTimeout="10" //seconds
       cookieless="UseCookies"
       cookieName="ASP.NET_SessionId" //Specifies the name of the cookie that stores the session identifier.
       timeout="20" //seconds
       regenerateExpiredSessionId="true" 
       useHostingIdentity="true">
      </sessionState>

or

session["user"]=textbox_username.txt

<configuration>
  <system.web>
    <sessionState cookieless="true"
      regenerateExpiredSessionId="true" />
  </system.web>
</configuration>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM