Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session.Abandon() does not abandon the session straight away

In my ASP.NET web app I call Session.Abandon() in Page_Load(). I would expect this would abandon the session straight away and the next time I reference the HttpContext.Current.Session a new session should be created. However, putting breakpoints on the Session_End and Session_Start handlers in Global.asax indicates that these aren't called until the page has finished rendering.

So two questions:

1) Why?

2) How can I continue to use HttpContext.Current.Session within a page lifecycle once Session.Abandon() has been called.

Thanks in advance!

like image 735
Mark Robinson Avatar asked May 23 '11 13:05

Mark Robinson


People also ask

What is Session abandon ()?

The Abandon method destroys all the objects stored in a Session object and releases their resources. If you do not call the Abandon method explicitly, the server destroys these objects when the session times out.

What is difference between session abandon and session clear?

Abandon() destroys the session. Session. Clear() just removes all values.

How do you abandon a session in Javascript?

The Abandon method destroys a user session. Note: When this method is called, the current Session object is not deleted until all of the script on the current page have been processed. This means that it is possible to access session variables on the same page as the call to Abandon, but not from another Web page.


2 Answers

http://msdn.microsoft.com/en-us/library/ms524310(v=vs.90).aspx

Look at the remarks section on the linked page. Looks like the session objects are only queued for deletion, and not deleted until the code finishes running.

like image 91
Elad Lachmi Avatar answered Sep 28 '22 04:09

Elad Lachmi


This was my solution:

private void PurgeSession()
{
    try
    {
        Session.Clear();
    }
    catch (Exception) {  }

    try
    {
        Session.Abandon();
    }
    catch (Exception) {  }

    try
    {
        Session.RemoveAll();
    }
    catch (Exception) {  }

    try
    {
        Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId") 
                                {Expires = DateTime.Now.AddYears(-1)});
    }
    catch (Exception) {  }
}

This is effectively the orbital bombardment option.

Some information sourced from: http://www.dotnetfunda.com/articles/article1395-how-to-avoid-the-session-fixation-vulnerability-in-aspnet-.aspx

like image 40
Chris Marisic Avatar answered Sep 28 '22 03:09

Chris Marisic