0

Possible Duplicate:
Is there any way to know that user leaving a page with asp.net?

How can I perform automatic logout or closing the session when leaving my web application?

(like GMAIL, when you doesn't check "remember me", that logs you off if you close the browser or move to other page).

Thanks, Inbal.

Community
  • 1
  • 1
Inbal
  • 909
  • 2
  • 28
  • 46

2 Answers2

0

you can use 'onbeforeunload' event and open a pop up to kill the session using Session.Abandon

or if you are using forms authentication you have an option to kill session after certain time using timeout

  • The problem with "onbeforeload" is that it called even when you navigate away from a page inside the web application, and I want the logoff to happen only when the user leaves the web application. – Inbal Oct 15 '12 at 12:38
  • it fires even when you close the window(when user leaves web application) , however there is a mixed response here, onbeforeunload doesnt behave the same way in all browsers – Chandra Sekhar Walajapet Oct 15 '12 at 12:39
  • Yep, but I don't want to close the session if the user just moves to another page... – Inbal Oct 15 '12 at 12:41
0
<script type="text/javascript">

        var clicked = false;
        function CheckBrowser() {
            if (clicked == false) {
                //Browser closed
            }
            else {
                //redirected
                clicked = false;
            }
        }

        function Logout() {
            if (clicked == false)//browser is closed
            {
                window.location = "../Master/Logoff.aspx";
            }
        }
    </script>
  1. Put this JavaScript on your head section of master page or content page

  2. Call logout method on onbeforeunload event of body part, such as

    <body onbeforeunload="Logout()" onclick="clicked=true;">

  3. Delete respected user entry from database and kill session on page_load event of called page such as:

Page Name: Logoff.aspx

Protected void Page_Load(Object Sender,EventArgs e)
{
         //Write Delete User From Database Code Here.
}

Pasted from Log out user on browser close

chridam
  • 100,957
  • 23
  • 236
  • 235