Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session ends randomly after redirect

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)

like image 469
Pavle Gartner Avatar asked Apr 29 '26 04:04

Pavle Gartner


1 Answers

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

like image 82
Clive Avatar answered May 01 '26 16:05

Clive



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!