How to close the session while browser close in asp.net
There is no direct event to call the while browser close or
tab closed, but we can achieve this functionality by using the javascript call
.
Step 1: - Use this code in the master page screen
<script language="javascript" type="text/javascript">
//<![CDATA[</span
/>
var clicked = false;
function CheckBrowser() {
if (clicked == false) {
//Browser closed
}
else {
//redirected
clicked = false;
}
}
function bodyUnload() {
if (clicked == false)//browser is closed
{
var request = GetRequest();
request.open("POST", "../loginOut.aspx", false);
request.send();
alert('This is close');
}
}
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>
And add this methods call for body part
<body onunload="bodyUnload();" onclick="clicked=true;">
</body>
Step 2 :- In logout.aspx page write the bellow code to close
the session
protected void Page_Load(object sender, EventArgs e)
{
Session.Clear();
Session.RemoveAll();
Session.Abandon();
}
No comments:
Post a Comment