Assigning Session to nothing or Session.Abandon

Posted by Joggee | ASP.NET Tips, ASp.NET 2005 | Wednesday 16 April 2008 11:37 am

I have seen so many developers once they wanted to destroy session, they used

Session(”A”) = Nothing

Its not a good practice to assign nothing object.

Correct Way:

Session.Abandon

The Session.Abandon method basically destroys a user session.

One thing I would add the current Session object will not delete untill the script on the current page have been processed completely.

Best Way:

FormsAuthentication.SignOut()
Session.Abandon()

Use Forms Authentication.Signout which will removes the forms-authentication ticket from the browser.

6 Comments »

  1. Comment by Naveed Mazhar — April 16, 2008 @ 11:42 am

    Very useful tip. I will definitely use it.

  2. Comment by Sohaib Ahmad — April 16, 2008 @ 11:44 am

    Wounderfull all most all the developers do so. Before readind this artical i also use same like
    Session(”A”) = Nothing
    Also FormsAuthentication.SignOut() is new thing for me.
    Thanks

  3. Comment by Lars Corneliussen — May 19, 2008 @ 6:29 pm

    Just some more notes. Use these three methods according to what you want to do! Think about it.

    1) Session.Remove(”A”) is more correct than Session(”A”) = null|Nothing, and it’s absolutely appropriate when you want to just clean up what you’ve put in there.

    2) Session.Abandon() removes ALL session values.

    3) FormsAuthentication.SignOut() removes the authentication ticket and redirects to the login page.

  4. Comment by Muhammad Adnan Amanullah — August 11, 2008 @ 6:26 pm

    don’t u think Session.Abandon() will destroy all session keys value whereas through Session(”A”) = Nothing we r just getting rid of Session(”A”) SOMEHOW

    just trying to share my thought, pls correct me if i m wrong.

    take care, Thanks,

  5. Comment by Kazelpnf — September 19, 2008 @ 11:27 am

    Hi webmaster!

  6. Comment by Alexei — December 6, 2008 @ 4:58 pm

    You are right Muhammad Adnan
    I think Joggee didnt explain it very well, but it is undestandable

    I think:
    Session(”A”) = nothing ‘Will set the variable to nothing, but the variable will still be in the session
    Session.Remove(”A”) ‘ Will remove the variable from the session
    Session.Abandon ‘ Will remove all variables from session, and i just wondering if a new session would be regenerated, bt not, i just tryed that, this only removes session variables but does not generate a new sessionid :)

RSS feed for comments on this post. TrackBack URI

Leave a comment