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)
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.
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.
</div>
Babasaheb Kale
Mark as Answer, if the post helped you...
Visit My Blog
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()"
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.
aahn77
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)
Thanks,
Zeeshan Umar ~Please Mark As Answer, one or multiple posts, which helped you. So that it might be useful for others~
aahn77
Member
12 Points
17 Posts
Session.Clear() or Session.Abandon() ?
Jun 13, 2011 07:24 AM|LINK
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)
Lateef045
Star
7813 Points
1541 Posts
Re: Session.Clear() or Session.Abandon() ?
Jun 13, 2011 07:30 AM|LINK
Check this link.
http://stackoverflow.com/questions/3021688/what-is-the-difference-between-session-abandon-and-session-clear-in-asp-net
http://stackoverflow.com/questions/1470445/what-is-the-difference-between-session-abandon-and-session-clear
anas
All-Star
73649 Points
7914 Posts
Moderator
Re: Session.Clear() or Session.Abandon() ?
Jun 13, 2011 07:31 AM|LINK
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.
aahn77
Member
12 Points
17 Posts
Re: Session.Clear() or Session.Abandon() ?
Jun 13, 2011 07:56 AM|LINK
thanks a lot for your guidance but please tell me with an example when do we should use Session.Clear()
Lateef045
Star
7813 Points
1541 Posts
Re: Session.Clear() or Session.Abandon() ?
Jun 13, 2011 08:01 AM|LINK
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.
Raigad
Contributor
5129 Points
956 Posts
Re: Session.Clear() or Session.Abandon() ?
Jun 13, 2011 08:36 AM|LINK
So, if you use
, 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
, 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. </div>Mark as Answer, if the post helped you...
Visit My Blog
aahn77
Member
12 Points
17 Posts
Re: Session.Clear() or Session.Abandon() ?
Jun 13, 2011 10:30 AM|LINK
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()"
sirdneo
All-Star
15171 Points
2509 Posts
Re: Session.Clear() or Session.Abandon() ?
Jun 14, 2011 12:29 PM|LINK
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.
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)
Zeeshan Umar
~Please Mark As Answer, one or multiple posts, which helped you. So that it might be useful for others~