Click here to Skip to main content
16,016,613 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi I want to remove all Session in close page event but I don't know how to do that?
I do that in click event for link button
please help me

my code:
C#
Session.RemoveAll();
       Response.Redirect("~/Default.aspx");
Posted
Updated 10-Jun-16 12:27pm
v2

1 solution

The browser close is a browser functionality, and is purely a client-side event that has nothing to do with your server-side code and will trigger no events to indicate that the browser was closed.You have to use onbeforeunload event to trigger a popup to notify users when the browser is about to close, You can then make an AJAX call to invoke a server-side method ( but you cannot guarantee that your server-side code will execute).

Also keep in mind that this event will also trigger when the user clicks on the hyperlink or attempts to close the browser window.

You might want to try decreasing the duration for your authentication cookie (if you are using Forms Authentication). This will essentially log your user out if they have not been active within the application past the duration of your timeout, which if set to a small value will likely expire prior to the user returning to the application.

You might also consider using a real-time technology like SignalR to essentially "track" a user throughout your application using their Connection ID and then if you see that they are idle after a certain duration, handle that accordingly and call the Session.Abandon() method.
 
Share this answer
 

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