Click here to Skip to main content
15,033,215 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to remove a specific session on closing the browser. The project is in with c#.

What I have tried:

I need to remove a specific session on closing the browser. The project is in with c#.
Updated 21-Mar-19 5:53am
Gerry Schmitz 21-Mar-19 11:32am
What does that mean? The "user" closes the "browser". You want to "expire" something?
bijeesh06 3-Apr-19 6:11am

You can't: there is no notification from the browser to your website that the browser is closing, or the page your site was displayed on has closed, or the user has navigated away from your site.

Even if there were, what would happen if the user pulls the mains lead out?
You can't because the server doesn't know the user has closed the browser. You'll just need to wait for the session to expire.
Nothing that is 100% reliable, but there are options, some more difficult than others.

1. Setting up WebSockets. Seems to be overkill if it's only going to be used for monitoring if the server has an active connection to the browser.

2. COMET. An AJAX based approach using a javasript timer to continuously ping the server to create a quasi long running http request. Once the page/tab is closed it will break the cycle and you will know this session is closed.

3. Javascript events onunload and onbeforeunload. Use the events to trigger an AJAX post notifying of session close. These would probably need to fire up an alert/confirmation immediately after the post so that the AJAX completes before the request is aborted.

4. "Intents". Those annoying javascripts that cause a modal window to pop-up when the mouse leaves the browing area. Again this would fire up an AJAX call.
Richard Deeming 21-Mar-19 15:01pm
But none of these would be able to differentiate between "the user closed the browser", "the user closed the tab, but has the site open in another tab", and "the user navigated to another page, possibly on the same site". :)
MadMyche 21-Mar-19 20:18pm
I do not disagree; and disclaimed it as unreliable.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900