Bala
Bala

Reputation: 1476

How does an ASP.NET session expire when the browser is closed?

As in the title, I wonder how a session expires when the client's browser is closed?

Upvotes: 2

Views: 3274

Answers (4)

Abel
Abel

Reputation: 57159

The session lives on the server. It expires when the browser is closed long enough or isn't used long enough or when a new request arrives that either doesn't contain the cookie, or the cookie refers to a sessionid that's too far in the past (default timeout is 20 minutes).

When there's no connection, the session is removed from memory at an undetermined moment in time, or when you programmatically call .Abandon on the session.

When a session is not available or a session has been cleared because it had timed out, a new session object will be created. When this is the result of a browser request, the Session_End event will trigger in the global.asax file.

Note: the actual way a session is timed out and cleared depends. I.e., inproc sessions will be destroyed and trigger a Session_Timeout. Out-of-proc sessions do not, and will be destroyed in a state server or an SQL server. In the latter case, a stored procedure is called regularly to clean up. The stored procedure is only called when there's activity on the server, which means that sessions can live longer than 20 minutes in (database) memory, but will be destroyed on next access.

Upvotes: 4

Jonas Elfström
Jonas Elfström

Reputation: 31428

It does and doesn't. It lives on on the server until it times out (usually 20 minutes). But since it's kept alive in the browser using a session cookie, that expires as the browser is closed, the user will not be able to reconnect to that session again.

Upvotes: 1

Ashley
Ashley

Reputation: 129

The browsers temporary cookies are deleted and the server kills the session data after a predetermined amount of time since last access.

Upvotes: 1

Neil Barnwell
Neil Barnwell

Reputation: 42125

As defined on the web server (e.g. IIS). The typical default is around 20 mins after the last access (i.e. web request for that session). At this point the session is cleared, so apps need to use either cookies or some server-side state to work out who someone is for return visits to make the experience seamless.

Upvotes: 1

Related Questions