简体   繁体   中英

how to update session timeout in web.config file form C# code?

I want to update session timeout from C# code

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

I use Session.Timeout=50 and that works fine but I want to change the value in Web.Config file.

After completing action file should be update like

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

When you use session.timeout=50 in your code. it won't effect to your config file. Instead of trying to change web.config how about the Global.asax file's Session_Start method and set Session.TimeOut to whatever you want.

More info :

https://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.timeout(v=vs.110).aspx

add these lines in global.asax

protected void Session_Start(object src, EventArgs e)
        {
            if (Context.Session != null)
            {
                if (Context.Session.IsNewSession)
                {
                    Context.Session.Timeout = 50;

                }
            }
        }

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