1

I have two questions. I will split my questions into two sections and give an overview. I'm not entirely sure of the code I need to post so if I have missed some out, please let me know and I will help.

Overview:

When the browser is closed I need to sign the user out.

Question 1:

How do I call Session_End when the browser is closed?

I did some reading on how to close it and it seems that the only way to detect if a Session has ended when a browser has been ended is by using something called 'InProc' in my web config. I gave it an attempt and it didn't seem to change anything at all. So I'm wondering if there is another way around this.

Session_End:

 protected void Session_End() {
            if (User.Identity.IsAuthenticated) {

            }
        }

Question 2: How to sign a user out on Session_End?

Once the program has called the void I need to sign the user out as it's causing a bug in my program. I am using the Authentification manager.

Session_End:

protected void Session_End() {
            if (User.Identity.IsAuthenticated) {
                AuthenticationManager.SignOut();
            }
        }
Andrew Kilburn
  • 2,251
  • 6
  • 33
  • 65

2 Answers2

2

Using InProc sessions, the Session_End method is called when the session times out. This is (by default) 20 minutes after the user has last accessed your site (requested a new page). Note that these 20 minutes could have been spent carefuly reading a single page of your site - his session is still closed.

There is no way to detect when a user has closed his browser or navigated away from your site.

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
0

You need to use the Global.asax file. It should already contain this Method, all you need to do is add the guts of your code.

protected void Session_End(object sender, EventArgs e)
{
    if (User.Identity.IsAuthenticated)
    {
        AuthenticationManager.SignOut();
    }
}  
thewisegod
  • 1,524
  • 1
  • 10
  • 11
  • Yes, but Session_End is still not called when the browser is closed. Apparently you cannot detect when a browser is closed and run the code anyway – Andrew Kilburn Aug 18 '15 at 12:02