Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to expire session on closing browser or tab in asp.net. I have tried following solutions which are not fulfill my requirement.Problem is that the unload method is called even when you navigate from one page to the other or when you refresh the page. So it invalidates the session every time the user navigates to another page.

What I have tried:

<big>Solution 1:</big>

<body onunload="bodyUnload();" Onclick="clicked=true;">

<script type="text/javascript">
var clicked = false; 
function bodyUnload() 
   {      
      if (clicked == false)//browser is closed  
          {   
         var request = GetRequest();  
           request.open  ("POST", "../LogOut.aspx", false);    
       request.send();    
        } 
   } 
 
   function GetRequest()  
     {       
     var xmlhttp;
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        return xmlhttp;
      } 
 
</script>

<big>Solution 2 :</big>


function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.myForm.time.value=xmlHttp.responseText;
}
}
xmlHttp.open("GET","customer_logout.php",true);
xmlHttp.send(null);
}
Posted
Updated 27-Feb-18 22:35pm
v2
Comments
Richard Deeming 28-Feb-18 12:35pm    
The answer hasn't changed since you asked basically the same thing two years ago:
https://www.codeproject.com/Questions/1069887/How-to-detect-browser-close-button-in-master-page[^]

1 solution

If this was possible don't you think asp.net would do it out of the box? No matter how many times this question is asked it still isn't possible because that's just how the internet works. You need to re-architect your solution to come up with something that doesn't require the session to end when the browser or tab is closed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900