user1189352
user1189352

Reputation: 3875

ASP.NET MVC - Session cookies seem to expire faster than timeout set?

I store some data in Session cookies.. like User ID for example. I set the timeout to expire in 60 minutes like so:

 <configuration>
  <system.web>
     <sessionState timeout="60"></sessionState>
  </system.web>
</configuration>

But there are times where it is expiring as fast as 10 minutes or so. I cannot figure out why. Is there something I am missing?

Upvotes: 1

Views: 1635

Answers (2)

Tommy
Tommy

Reputation: 39807

The default session provider for IIS/.NET is InProc, which is short for In Process (i.e. volatile memory). What this means is that, if for any reason, the process restarts, all session data will be lost as the data stored within the process is lost when the executable restarts / is shut down. IIS web applications will shut down after some period of time by default, which would cause all session data to be lost. This can also include starting and stopping debugging sessions (per comment below).

Another possible issue would be if you are deploying to a server farm. InProc sessions cannot be shared across multiple web servers.

If you are deploying to a single server and you can control the configuration, check to see how often the process (App Pool) is set to recycle or timeout. You may want to increase this timeout period to better suit your session requirements.

If you are deploying to a web farm (more than one machine or cloud hosted) or cannot control the server configuration, then you should look at implementing something like Sql Session State provider in your application. This will provide a sole source for storing session data that can be shared between multiple web servers as well as the data is stored independent of the running process, so it will survive process restarts/crashes.

Upvotes: 1

10K35H 5H4KY4
10K35H 5H4KY4

Reputation: 1526

Try to Log reason for your session expire

Set web.config

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

On global.asax

void Session_End(object sender, EventArgs e)
    {
        // Write your code to capture log

    }

Upvotes: 1

Related Questions