Closing the browser should actually clear the session variables by the ASP.net session identifier

I have an ASP.net page. When I close the webpage, I need to clear the session variables.

To handle this, I need to maintain a timeout of up to 20 minutes. If he closes and enters the login for any number of times within 20 minutes, the timeout

Is there any way to clear the ASP.net session id

+4
source share
6 answers

[EDIT] As suggested by others, your session should end, but if you want to close the session before the timeout (for example, to clear large objects of the session) And you have javascript ...

You can do this with the window.onbeforeunload handler, which is sent to the exit page.

 function CloseSession( ) { location.href = 'SignOut.aspx'; } window.onbeforeunload = CloseSession; 

You can also do this through AJAX. The SignOut.aspx page (or any other platform you use) should refuse a user session.

+5
source

You cannot do this.

  • You cannot rely on an event happening in the browser.
  • The server cannot contact the browser to find out if it is alive.

You do not control the end user browser. The user can close the browser, crash, disconnect from the Internet, disable or disable javascript, etc. Etc. Or, the user can simply enter a different URL (and after that the user can return to your page using the "Back" button and wait for the continuation of his session from your site).

Launch window.onbeforeunload starts every time the page is unloaded. Even if the user views another page of your site, therefore, a second session may be required.

+11
source

I think the only good way to do this is to set a timeout on the session variables on the server. The web server cannot know for sure whether the user has closed the browser or not.

Javascript is not a good option, since you can disable it on the client side.

+5
source

What if the browser process ends?

Here is the trick

You should not have so many session variables that they need to be cleared. See this article for other options.

+2
source

I don't know how ASP.Net implements its sessions, but part of the RFC cookie is that session cookies (those that do not have a specific expiration date) are cleared whenever the browser is closed.

Of course, is there a way to get ASP.Net using the correct session cookies, rather than with a fixed timeout?

0
source

Hi try this javascript function

 function ConfirmClose() { if (event.clientY < 0) { /* Your asynchronouse request to the page and call that function*/ } } /*now call this method on event of javascript*/ 

I did not understand this, but he just got out of my head

-1
source

All Articles