ASP.NET session expires before timeout value specified in Web.Config

Note: You can refer this article to know more about different ways to store session state.

There are number of reasons why values stored in session being lost before session timeout value set in web.config.

Are you using In-Proc Session State ?  If Yes, check out following things.

Reason# 1: If ASP.NET encounters any of the following changes, it will recycle worker process and all state data for applications that are stored in process will be lost(so session expires).

  • Machine.Config, Web.Config or Global.asax are modified
  • The bin directory or its contents is modified
  • The number of re-compilations (aspx, ascx or asax) exceeds the limit specified by the <compilation numRecompilesBeforeAppRestart=/> setting in machine.config or web.config  (by default this is set to 15)
  • The physical path of the virtual directory is modified
  • The CAS policy is modified
  • The web service is restarted
  • (2.0 only) Application Sub-Directories are deleted (see Todd’s blog for more info)

Reason# 2: There are few settings in IIS that can cause application pool or worker process to be recycled.

1 ) Right click on application pool that being used for your website and go to properties and check out the following options.

IIS recycle the worker process after certain time period or after X number of requests if those options checked as shown in above screen shot that can cause your session being lost.  Please keep those options in mind and set appropriately based on your need.

2) Go to performance tab and check out following option.

If the option shown in above screen shot checked, please make sure that time set over there match the session timeout value set in web.config.

3) Right on your website -> Properties -> Home Directory -> Configuration -> Options Tab.

This option is only applies to classic ASP application.  So don’t worry about the value set over there.

Reason# 3: If you are using more that one web server(web farms), it is important to remember that all your web requests will not stick to particular web server. For example, if you are making a web request(request# 1), that request will be served by web server 1.  Your another web request(request# 2) may be served by another server web server 2.  Since you are using In-Proc session state, session gets stored in In-Memory of worker process.  So in this case session data gets stored across web server and in incosistant state.  So if you have web farms setup, you need to use Out-Proc session state(StateServer or SQL Server). Don’t know how to setup Out-Proc session state?  This post might help you.

Hope this helps !

Posted in Microsoft Technology Tagged with: