3

I have a strange problem in ASP .NET MVC 4. In AccountController I am doing redirects on certain actions and put data into TempData (which is stored in Session) before that:

[AllowAnonymous]
public ActionResult Activate(string token)
{
    new CustomSignupService().Activate(token);
    TempData["Message"] = "User was successfully confirmed";
    return RedirectToAction("Message", "Home")
}

Now I know I could just return shared Message view in this case, but this is just a code sample to reproduce the problem.

CustomSignupService.Activate does a db lookup via NHibernate and updates user in transaction (user activation). Sometimes (lets say 1/5 tries in 5 minutes) TempData does not make it throught the redirect, so I added logging into Session_End and noticed that session ends when RedirectToAction is invoked. Right after that Session_Start is invoked but of course TempData is gone.

Session has default timeout (20min) and controllers use SessionStateBehavior.ReadOnly

Any ideas?

UPDATE

Step 1: It's not Application Pool recycling (I turned on all General Recycle Event Log entries on Application pool and checked event log, after session restarts but recycle is not causing it)

Pavle Gartner
  • 659
  • 1
  • 7
  • 21

2 Answers2

3

I had a problem with Session being lost. I used Fiddler and noticed that there was a duplicate ASP.NET session cookie with a blank value. I don't know how it got there. So a new session was created on every request. I deleted that duplicate cookie and the problem was solved.

Other unlikely reasons are:

IIS process recycle

Session.abandon being called

modifying bin folder or web.config causing app restart

Check out this page:

Losing Session State

Community
  • 1
  • 1
Clive
  • 1,128
  • 2
  • 11
  • 19
1

I don't know ASP.NET MVC but in the dark centuries I used ASP.NET without MVC. I struggled several times with unexpected session ends. Most of the time it was caused by some simple things which are described in the article http://www.c-sharpcorner.com/uploadfile/technoNet/session-timeouts-causes-and-remedies/

Sometimes the server has entries in the event log that gives you a little bit more information.

And some other solutions might be

ASP.NET Session ending abruptly

random IIS session timeout

Community
  • 1
  • 1
Kai
  • 1,953
  • 2
  • 13
  • 18