Search code examples
asp.netsessionweb-configsessionend

Session_End doesn't call after i close browser


I use below setting in web.config

<sessionState mode="InProc" cookieless="false" timeout="10"/>

...

<authentication mode="Forms">
<forms loginUrl="login.aspx" timeout="10"/>

and in Session_End according to Session's value i change a column's value in database (if this column's value is 1 means user online) but sometimes after a day that user close browser this column's value steel is 1 (means Session_End doesn't call after user close browser)


Solution

  • No Session_End will not call on page close. You need to use script for that

    <script>    
    window.onbeforeunload = function (evt) {
    var message = ‘Are you sure you want to leave?’;
    if (typeof evt == ‘undefined’) {
    evt = window.event;
    }
    if (evt) {
    evt.returnValue = message;
    }
    return message;
    }
    
    </script>
    

    Using this script you can identify when user closes the browser