开发者

set the timeout for a specific session

开发者 https://www.devze.com 2023-03-29 04:49 出处:网络
How I can set the timeout for a session specific in ASP.NET? it is possible? I have开发者_StackOverflow two or more sesions in my web application, I need set:

How I can set the timeout for a session specific in ASP.NET? it is possible? I have开发者_StackOverflow 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


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>


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

0

精彩评论

暂无评论...
验证码 换一张
取 消