locked
Session.Clear() or Session.Abandon() ? RRS feed

  • Question

  • User1707988136 posted

    Hi friends

    we khnow The Abandon method is used to clear out session data and raise the Session_End event defined in the Global.asax and The clear method Clears all keys & values from the session-state item collection.but when do we should use Session.Clear() instead of Session.Abandon() ?     (please an example)

    Monday, June 13, 2011 3:24 AM

Answers

  • User-693248168 posted

    You use Session.Clear() when you dont want to end the session but rather just clear all the keys in the session and reinitialize the session.

    Session.Clear() will not cause the Session_End eventhandler in your Global.asax file to execute.

    But on the other hand Session.Abandon() will remove the session altogether and will execute Session_End eventhandler.

    Session.Clear() is like removing books from the bookshelf

    Session.Abandon() is like throwing the bookshelf itself.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 13, 2011 4:01 AM

All replies

  • User1564875471 posted

    Session.Clear() clears out all the values stored in the session but it will keep the user session, so the session_end event will not be fired.Session.Abandon() kills user session and so the user will be assigend a new session id next time he/she visit the site.

    Monday, June 13, 2011 3:31 AM
  • User1707988136 posted

    thanks a lot for your guidance but please tell me with an example when do we should use Session.Clear()

    Monday, June 13, 2011 3:56 AM
  • User-693248168 posted

    You use Session.Clear() when you dont want to end the session but rather just clear all the keys in the session and reinitialize the session.

    Session.Clear() will not cause the Session_End eventhandler in your Global.asax file to execute.

    But on the other hand Session.Abandon() will remove the session altogether and will execute Session_End eventhandler.

    Session.Clear() is like removing books from the bookshelf

    Session.Abandon() is like throwing the bookshelf itself.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 13, 2011 4:01 AM
  • User-366017857 posted

    Session.Abandon() destroys the session and the Session_OnEnd event is triggered.

    Session.Clear() just removes all values (content) from the Object. The session with the same key is still alive.

    So, if you use Session.Abandon(), you lose that specific session and the user will get a new session key. You could use it for example when the user logs out.

    Use Session.Clear(), if you want that the user remaining in the same session (if you don't want him to relogin for example) and reset all his session specific data.

    Monday, June 13, 2011 4:36 AM
  • User1707988136 posted

    thanks a lot for your guidance but when session timeout is finished or we call Session.Clear() the user must relogin. what do you mean about "if you don't want him to relogin use Session.Clear()"

    Monday, June 13, 2011 6:30 AM
  • User-481631678 posted

    Session.Clear removes all the variables stored in session and if user try to browse your site same sessionID will be used which was previously assigned to him.

    Session.Abandon removes all the variables stored in session, fire session_end event and if user try to browse your site a new sessionID will be assigned to him.

    what do you mean about "if you don't want him to relogin use Session.Clear()"

    Either you call Session.Clear or session.Abandon, user will logout if you are using session to store his login status (almost in 99.99% sites session is user to login/logout users)

    Tuesday, June 14, 2011 8:29 AM