set the timeout for a specific session

前端 未结 4 449
旧时难觅i
旧时难觅i 2021-01-22 14:13

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\"] //exp         


        
相关标签:
4条回答
  • 2021-01-22 14:41
        <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>
    
    0 讨论(0)
  • 2021-01-22 14:48

    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 讨论(0)
  • 2021-01-22 14:55

    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

    0 讨论(0)
  • 2021-01-22 14:58

    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

    0 讨论(0)
提交回复
热议问题