Answered by:
Clear the session on browser close

Question
-
User1082734430 posted
Hi,
Can you please suggest me the approach how can i clear the session on browser close.
I googled it and got many links, i tried to use "onbeforeunload" event in jquery. But this method is called even on redirection of the page. So i need to identify is it a browser or a redirection. In case of redirection i don't wanna do anything.
I also tried Application_End and Session_End events but of no use.
Please suggest me the approach how can i go ahead.
Wednesday, August 20, 2014 1:05 PM
Answers
-
User753101303 posted
Hi,
IMO the best option is to educate your users (on the login page) so that they use the "logout" link made available to them. Basically you have no way to know (at least a reliable way) that the user closes the last window on your site.
Perhaps later (if I remember there is a Windows api being worked on).
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 20, 2014 1:31 PM
All replies
-
User753101303 posted
Hi,
IMO the best option is to educate your users (on the login page) so that they use the "logout" link made available to them. Basically you have no way to know (at least a reliable way) that the user closes the last window on your site.
Perhaps later (if I remember there is a Windows api being worked on).
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 20, 2014 1:31 PM -
User-183374066 posted
Answer many times before on same forums .. Refer following
http://forums.asp.net/t/1621725.aspx?Clearing+Session+Variables+on+browser+close
http://forums.asp.net/p/1616771/4140915.aspx#4140915?Re%3a+Session+expire+on+window+close
http://forums.asp.net/p/1504428/3567686.aspx#3567686?Re%3a+Browser%27s+close+button+event
http://forums.asp.net/p/1462992/3367367.aspx#3367367?Re%3a+Detect+Browser+Close+event
http://aspalliance.com/1294_CodeSnip_Handle_Browser_Close_Event_on_the_ServerSide.all
Or Google it
Wednesday, August 20, 2014 1:40 PM -
User-219472588 posted
My 2 cents,
- If you are storing session in memory (default option) then I afraid there is no perfect solution. You can go what Naseer mentioned.
- If you are storing session in SQL database then you can clear session via Job (handled by SQL Server Agent).
But before that, you should ask yourself why you need to clear session on browser close? Is large session item object affecting the scalability of the system? Is yes then it's better to find alternative solution.
Wednesday, August 20, 2014 7:47 PM -
User831885415 posted
truly speaking, there is no way of distinguish between browser close and normal navigation using unload event,
only thing u can do is, set a flag at client side on click of any navigation and check if that flag is true in unload event then it is navigation unload. in this case let the unload happen.
else ask to leave or not, on leave do service call to server to end the session
Thursday, August 21, 2014 12:07 AM