0

i need to sign out an authenticated user in ASP.NET reliably when a browser tab gets closed. What is the recommended solution?

Thanks

Faris Zacina
  • 14,056
  • 7
  • 62
  • 75
  • (Duplicate) Take a look at this answer, it covers the same question: http://stackoverflow.com/a/287027/328968 – Peter Jun 23 '12 at 14:10

2 Answers2

0

Typically you would do your sign-out logic when the session has ended. But if you had to detect when page is closed, use this:

<body onunload="performMyLogoutLogic();">
...
...
</body>
Jeremy
  • 8,902
  • 2
  • 36
  • 44
  • 1
    This will also log the user out if they merely navigate away from the page, say, to a different page on the same site. – Jon Sagara Jun 22 '12 at 21:17
-1

You could use a generic handler to kill session and call this beforeunload like this:

function CloseSession( )
{
    location.href = 'KillSession.ashx?task=1'; 
}
window.onbeforeunload = CloseSession;

And in your KillSession.ashx do this

 public void ProcessRequest(HttpContext context)
            {
                if(!String.IsNullOrWhiteSpace(Request.QueryString["task"].toString()))
                {
                 if(Request.QueryString["task"].toString()=="1")
                  {
                    Session["User"]==null;
                    context.Response.ContentType = "text/plain";
                    context.Response.Write("Good Bye!");
                   }
                }
            }
Ashwin Singh
  • 7,197
  • 4
  • 36
  • 55