Asp.net mvc session often expires in a hosting environment

In case someone has another problem, and it has been resolved, I would like to share my solution:

Problem: I have a website made in asp.net mvc3, the session lasts often, between a few seconds and up to 5 minutes. In web.config, I set the timeout to 2880 minutes, I also set sessionstate to stateerver. I also delete timeouts for default use, still the problem:

<sessionState mode="StateServer"></sessionState> <authentication mode="Forms"> <forms loginUrl="~/Account/LogOn"/> </authentication> 

I made a sample web site that contains only login functions and interacting with databases to simulate an environment, then I got a more stable timeout of 2 minutes. 50 sec (checked 9 times), which is still not correct, since I set 2880 minutes in the web.config file.

Inside the code, when a user logs in, I create an AuthenticateTicket and save it in cookies, and then on the secure pages that I checked for User.Identity and the controllers have a [Authorize] filter, so when the session ends, I go to the input page.

On the same hosting server, I have other old applications that use .NET 3.5 web forms, and the session is fine for them, only for new mvc3 and mvc3 the session often expires. Thanks for any help you can give.

+4
source share
3 answers

The final:

My particular problem was that my hosting (Arvixe) set a limit of 100 MB of memory for each website. Using EF (and probably any ORM) increases memory usage due to complex queries mapped to the database, so my sites used more than 100 MB, which caused the application pool to restart due to a 100 MB memory limit, so Session was reset.

Solutions:

  • Optimize your MVC website ( see this post )
  • Use a different way to save a session, perhaps by storing it in an SQL database.
  • Switching to a virtual or dedicated server

In my specific case, setting up each website to use a shared application pool solves the problem temporarily, while I follow the 1st option message to optimize my websites, so I can go under the Arvixe limit of 100 MB of memory.

+3
source

I had a similar problem, my session stopped for about 30 seconds, this only happened in my development environment. I found my solution here: http://forum.winhost.com/showthread.php?t=9017

Try adding your car key to your website.

+2
source

This can be a common problem when using shared hosting for the following reasons:

You share the server with other clients, and the ASP.NET workflow often restarts.

Your host uses load-balanced servers, and you visit one server, and then on the next visit, you click another server that does not have session data.

Best practices:

1.If you have an SQL database with your host, look at setting up the database for general session state.

2. When writing code for a long time, they are independent of the values ​​in the session β€” first check, and if the object is not in the session, rebuild the object.

3, Consider switching to VPS or updating your plan.

+1
source

All Articles