0

I'm working on an asp.net project (written awhile ago), and users say they want to be able to log out on "x'ing" out the tab or browser.

I've been looking around this site and have tried a few things with no luck. It doesn't seem like there is an easy way about this with there being different browsers.

First, here's a logout function which is called from a logout button.

public void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e)
{
    Response.Cookies.Clear();
    Session.Clear();
    HttpContext.Current.User = null;

    FormsAuthentication.SignOut();
}

It would be nice if I could somehow call that when closing the tab/browser.

There is this javascript in the master page:

<script type="text/javascript">
    // Copyright 2006-2007 javascript-array.com

    var timeout = 500;
    var closetimer  = 0;
    var ddmenuitem  = 0;

    // open hidden layer
    function mopen(id)
    {   
        // cancel close timer
        mcancelclosetime();

        // close old layer
        if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

        // get new layer and show it
        ddmenuitem = document.getElementById(id);
        ddmenuitem.style.visibility = 'visible';

    }
    // close showed layer
    function mclose()
    {
        if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
    }

    // go close timer
    function mclosetime()
    {
        closetimer = window.setTimeout(mclose, timeout);
    }

    // cancel close timer
    function mcancelclosetime()
    {
        if(closetimer)
        {
            window.clearTimeout(closetimer);
            closetimer = null;
        }
    }

    // close layer when click-out
    document.onclick = mclose; 
</script>

However, it doesn't seem to work. I messed around w/ the timeout variable and no matter how long I wait after I exit the tab/browser, the cookies/session do not clear out. I know chrome has a "continue where you left off" feature, and other browsers have similar I'm sure. Unfortunately, all of the users don't use the same browsers.

Any ideas would be most welcome!

pfinferno
  • 1,779
  • 3
  • 34
  • 62
  • 2
    Basically you can't do it, whenever the user closes the window the page cannot execute any code, so you can't do what you want to do. But, you can use a Session cookie, which will expire when the user closes the browser. Take a look at this: http://stackoverflow.com/questions/3737285/set-cookie-to-expire-at-end-of-session-asp-net – Gusman Apr 05 '16 at 18:02
  • 2
    The cookies will stay until the browser closes because the browser instances share memory. If you specify an expiration, they can be written to disk possibly depending on the expiration length, but otherwise they are in shared memory. – Mark Fitzpatrick Apr 05 '16 at 19:06

0 Answers0