2

I have such a requirement for my ASP.NET MVC app:

Session created before authentication should be closed and new session, with new ID, should be started

So it should work like this:

  1. User is redirected (GET request) to my app (some method in the controller) with auth info
  2. Closing old session, starting new one
  3. Auth, saving user data to the session

Let's look to the controller:

public ActionResult Login(string token) {
    Session.Abandon(); // I want new session!

    // Some auth stuff

    Session["this"] = "x";
    Session["that"] = "y";

    return View();
}

On debugging, i can see session values "this" and "that" set to "x" and "y". But now let's go the view that this method uses:

@{
    var @this = Session["this"]; // Wut? @this is null
    var that = Session["that"]; // Wut? that is null
}

So, a little digging gave me the answer:

Abandon causes the End event to be raised. A new Start event will be raised on the next request. https://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.abandon%28v=vs.110%29.aspx

It seems that lifecycle looks like this:

  1. "Session.Abandon()" - session marked to be closed
  2. Setting session values.
  3. "return View()"
  4. Session closed and (not sure about this) new session started
  5. View - session values set in controller are missing.

Completly unacceptable. How can I overcome this?

Andrzej
  • 848
  • 1
  • 11
  • 26
  • Would [generating a new Session Id](http://stackoverflow.com/questions/11987579/how-to-generate-a-new-session-id) fulfil your requirements? – stuartd Jun 06 '16 at 10:19

1 Answers1

0

I hope this code will solve your issue;

var XYZ= HttpContext.Current.Session["this"]; 

OR You can use 'TempData' for your purpose.

  var XYZ== TempData["this"].ToString();