Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this possible to clear the session whenever browser closed in asp.net?

In my asp.net application, i want to clear the session whenever my browser closed or my tab (if my browser containing multiple tabs)closed.

Please guide me to get out of this issue...

like image 957
Saravanan Avatar asked Nov 06 '12 09:11

Saravanan


3 Answers

Short version, No. There's no solid way of a server detecting if the client has closed their browser. It's just the nature of web development's asynchronous pattern.

Long version, if it's really, really important to you; Put a bit of javascript in the page that sends a regular post to your website in the background and set up a serverside agent or service that disposes of the sessions if it doesnt receive these regular "heartbeat" signals.

You can put a javascript postback onto the page's unload() event but dont rely on it, it doesnt always fire.

like image 55
PaulMolloy Avatar answered Sep 22 '22 22:09

PaulMolloy


This happens by default whenever you close your browser, and that's not just for ASP.NET. It's for most server-side programming languages that have a session state. Basically, any cookie that is added that doesn't specify an expiration date, will be deleted when the browser is closed.

Where this doesn't apply, is when you close a tab, which is something you will not have any control over because the tab close event will not get sent back to the Web server.

like image 33
Lawrence Johnson Avatar answered Sep 23 '22 22:09

Lawrence Johnson


You can try to do that with javascript. Check it at:

http://www.codeproject.com/Tips/154801/How-to-end-user-session-when-browser-closed

like image 41
Jordi Avatar answered Sep 22 '22 22:09

Jordi