How to prevent session timeout

20,641

Solution 1

Here is one way:

http://weblogs.asp.net/stevewellens/archive/2009/06/09/ah-ah-ah-ah-staying-alive-staying-alive.aspx

Sometimes you want your web page to 'stay alive'. That is, if a user is filling out a complicated form, you do not want the session to time out before they are finished. The user could get very angry and rightfully so: You might even get yelled at!

It's not simply a matter of increasing the session timeout to a very large value. If you do that, the sessions would be left active in the server memory for hours—long after the visitors have left the site. Increasing the session timeout IS a solution… but not necessarily a good solution.

The goal is that the session should stay active as long as the web page is open on the client machine …even if there are no post backs to reset the session timer. When the web page is closed, the session should time out normally.

I implemented a solution for this: The client will "ping" the server at intervals of less than the session timeout which will reset the session timer. This is known as the Heartbeat design pattern (I couldn't find a decent site/page to link to)...

Solution 2

We use SQL Server to preserve session state. http://idunno.org/articles/277.aspx

This has the additional advantage of working in server farms.

These articles will also be helpful for you:

http://msdn.microsoft.com/en-us/library/ms178586.aspx

http://support.microsoft.com/kb/317604

For really easy-to-follow videos, try this search: http://www.bing.com/videos/search?q=asp.net+session+state+&go=&qs=n&sk=&sc=2-19&form=QBVR#

Share:
20,641
Michael Tot Korsgaard
Author by

Michael Tot Korsgaard

SOreadytohelp Language: C# ASP.NET / MVC HTML/HTML5 JavaScript/Jquery CSS/CSS3

Updated on July 09, 2022

Comments

  • Michael Tot Korsgaard
    Michael Tot Korsgaard almost 2 years

    I know this is probably an easy question for most of you guys. But my problem is that my server host empty their session pools every minute. So how do I get my users to stay logged in longer than one minute?

    I've heard that I could use SessionStates, but I haven't found any guides on the net that's easy to use for a newbie like me.

    Also I've heard about doing it with cookies. How?

    I'm working with C# and .Net

  • David
    David over 12 years
    +1 not only because it's a good solution, but also for the groovy title.
  • Mahmood
    Mahmood almost 2 years
    I'm using ASP.NET 5.0, I had the same issue, I was inspired by @SteveWellens answer, and the problem was solved by calling a C# method (server) with Ajax (client) that is repeated every 5 minutes.