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)
{
var request = GetRequest();
request.open ("POST", "../LogOut.aspx", false);
request.send();
}
}
function GetRequest()
{
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else {
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);
}