Home » Asp.netRSS

Kill session when we try to close the browser without logging out in all possible ways

Hi I need to kill the session and log the excetions when we try to close the browser without logging out

when we try to close the browser using alt-f4, browser close(X) and right click on browser and close -> this all is done and is working fine.

Now I need to handle the events

1. when we try to close the browser using Task Manager and kill the iexplore.exe.[ I need some event so that I can kill the session and log the event]

2. when we try to close the browser using right click->close from windows task bar [I need some event.clientY so that I can kill the session]

Kindly help me in resolving this Issues

Regards,

Nagesh

 

 

13 Answers Found

 

Answer 1

 Hi Nagesh,

I'm afraid this is a difficult task.

As we know, HTTP is a stateless protocol, the browser  doesn't keep connecting to the server. It's not possible to tell the server that the browser is closed.

I think you may try an alternative option.

1. Set the timeout of session  to a very small value, say 1 minute.

2. Add a hidden iframe to every page, it will access the server at a interval shorter than 1 minute

As a result, if a browser is closed no matter how, its session will be expired in a minute.

Hope this helps. 

 

Answer 2

You can write code in window.onunload event in JavaScript. Inside this event, write a code which will detect whether user has closed the brower or not by clicking X button.

 

window.onunload = function ()

{

 if((window.event.clientX<0) && (window.event.clientY<0)) {
      
       window.open("logoff.aspx");     
         
      }

}

 

Answer 3

Hi nagesh

 I have the same problem that you said you had a fix for it.(using alt-f4, browser  close(X) and right click  on browser and close  )

Would you please kindly  provide code example for us. Thanks

 

Answer 4

hi

 <bodyonunload="HandleOnClose();">

//The following is the javascript code, when the user tries to close  the browser  using (X), this function is called in body tag

function HandleOnClose(e)

{

if (event.clientY < 0)

{

if((document.getElementById('ctl00_c1_signIn')!=null) || (document.getElementById("ctl00$c1$Login1$LoginButton")!=null))

{ sendXMLHTTRequest(100);}

else

{sendXMLHTTRequest(101); }

}

}

//The following is the javascript code, the function catches the ID(i.e whether the user is logged in or not) here I am using XMLHTTPREQUEST so that I can make the session  abandon, without postback(manual AJAX), so here no need to use any popup window to close the sessions.

function sendXMLHTTRequest(ID)

{

/*100-window close without user logged In

101- browser close after the user is logged In 

*/

if(ID >100)

{

//alert('ID='+ ID);

var objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");

objXMLHttp.open("POST", "../Common/SessionTracker.aspx?ParamID=" + ID , false);

// in sessiontracker.aspx I am killing the sessions

objXMLHttp.send(new ActiveXObject("Microsoft.XmlDOM")); // Passing the XMLDom Object is required

}

}

//The following is the javascript code, when the user tries to close the browser using alt-f4

document.onkeydown = function()

{

if(window.event.keyCode == 115 && event.altKey == true)

{

if((document.getElementById('ctl00_c1_signIn')!=null) || (document.getElementById("ctl00$c1$Login1$LoginButton")!=null))

{sendXMLHTTRequest(100);}

else

{sendXMLHTTRequest(101);}

}

}

 

 

Answer 5

 Hello,

why we can't try with

protected void Session_End(Object sender, EventArgs e)
        {

        }

and set the session  time required

 

think this ll help 

 

Answer 6

Because if they close  the browser  it will expied in 20 min ( default) and It is not good enough for some applications. For example I have asp application and I count the number of users login to mu website for each client and they are limited to 2 for example.

so if a user of my client login to the application and close the browser 2 times they do not have privelage to access untill 20 min ( session  timeout) so I want to force them some how.

 Thanks

 

Answer 7

Thanks for your your reply. Your code help  me a lot. The only case is left for me is if user navigate away from my application.

How did you deal with that?

Thanks again

 

 

Answer 8

What if they go to file -> exit menu and close  the browser. you code does not handle  it.

 

Answer 9

Hi if you have the code for detecting browser close  "using alt-f4, browser  close(X) and right click  on browser and close -> " then please send it me plz

 

Regards,

Pavan

 

Answer 10

Hi Pavan

I have already posted the code for the above requirement. I am attaching the code.

hi

 <bodyonunload="HandleOnClose();">

//The following is the javascript code, when the user tries to close  the browser  using (X), this function is called in body tag

function HandleOnClose(e)

{

if (event.clientY < 0)

{

if((document.getElementById('ctl00_c1_signIn')!=null) || (document.getElementById("ctl00$c1$Login1$LoginButton")!=null))

{ sendXMLHTTRequest(100);}

else

{sendXMLHTTRequest(101); }

}

}

//The following is the javascript code, the function catches the ID(i.e whether the user is logged in or not) here I am using XMLHTTPREQUEST so that I can make the session  abandon, without postback(manual AJAX), so here no need to use any popup window to close the sessions.

function sendXMLHTTRequest(ID)

{

/*100-window close without user logged In

101- browser close after the user is logged In 

*/

if(ID >100)

{

//alert('ID='+ ID);

var objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");

objXMLHttp.open("POST", "../Common/SessionTracker.aspx?ParamID=" + ID , false);

// in sessiontracker.aspx I am killing the sessions

objXMLHttp.send(new ActiveXObject("Microsoft.XmlDOM")); // Passing the XMLDom Object is required

}

}

//The following is the javascript code, when the user tries to close the browser using alt-f4

document.onkeydown = function()

{

if(window.event.keyCode == 115 && event.altKey == true)

{

if((document.getElementById('ctl00_c1_signIn')!=null) || (document.getElementById("ctl00$c1$Login1$LoginButton")!=null))

{sendXMLHTTRequest(100);}

else

{sendXMLHTTRequest(101);}

}

}

 

 

Answer 11

Hey, Thanks a lot.

 

Answer 12

 Dear friend 

Thanks for posting the browser close  event code.Your code works even when u close the browser  by task  manager and for other options I have solved the issues .If you have got the solution how to handle  the browser close when user right clicks the browser and choose the close option please send it.

Regards

Chandrakant,

 

 

Answer 13

Pls explan clearly

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter