How to kill the session when user closes browser in asp.net 2.0
-
Hi friends, We are developing a web application that uses sessions and Forms authentication. I want to call Session.Abandon() to kill the session when the user closes the browser window(from any page of the application after logging in). How to achieve this? Please shed some light on this to help me. Thank you, Shah_tech
-
Hi friends, We are developing a web application that uses sessions and Forms authentication. I want to call Session.Abandon() to kill the session when the user closes the browser window(from any page of the application after logging in). How to achieve this? Please shed some light on this to help me. Thank you, Shah_tech
-
Hi friends, We are developing a web application that uses sessions and Forms authentication. I want to call Session.Abandon() to kill the session when the user closes the browser window(from any page of the application after logging in). How to achieve this? Please shed some light on this to help me. Thank you, Shah_tech
Hi You can call a javascript function on closing the browser. In that javascript function navigate to a logout page where you can then kill the session.
function AlertForBrowserClose()
{var clicked=false; var winClosed=false; if (window.screenTop&&(window.screenTop>screen.height)) { winClosed=true; } if ((!clicked) && (!winClosed)) { //This part wiil be excuted OnClick of REFRESH } else if(window.event.clientY < 0 && window.event.clientX < 0) { //This part wiil be excuted OnClick of BROWSER Close(X) var MCPageName = document.location.href.substring ( document.location.href.length , document.location.href.lastIndexOf('/') + 1 ); if(MCPageName!="MCAlerts.aspx") { window.location.href = '../logout.aspx'; alert('Please use proper "LogOut" for next sessions!'); } } }
I have used alert statement alert('Please use proper "LogOut" for next sessions!'); Make sure that this alert is used. Else the logout page will not get hit and session soes not end.
Naina
-
Hi You can call a javascript function on closing the browser. In that javascript function navigate to a logout page where you can then kill the session.
function AlertForBrowserClose()
{var clicked=false; var winClosed=false; if (window.screenTop&&(window.screenTop>screen.height)) { winClosed=true; } if ((!clicked) && (!winClosed)) { //This part wiil be excuted OnClick of REFRESH } else if(window.event.clientY < 0 && window.event.clientX < 0) { //This part wiil be excuted OnClick of BROWSER Close(X) var MCPageName = document.location.href.substring ( document.location.href.length , document.location.href.lastIndexOf('/') + 1 ); if(MCPageName!="MCAlerts.aspx") { window.location.href = '../logout.aspx'; alert('Please use proper "LogOut" for next sessions!'); } } }
I have used alert statement alert('Please use proper "LogOut" for next sessions!'); Make sure that this alert is used. Else the logout page will not get hit and session soes not end.
Naina
-
Thank you for quick reply. One thing is should I write the code in all the aspx pages of my application? How can we kill the session when the browser is closed without writing the code in every page? Thank you, Shah_tech
Are you using master page in your application which is referred in all the aspx pages. If so you can write the code in master page and redirect to logout page. If not i guess you should write in every aspx page. Regards Naina
Naina
-
Are you using master page in your application which is referred in all the aspx pages. If so you can write the code in master page and redirect to logout page. If not i guess you should write in every aspx page. Regards Naina
Naina
or just put it in a .js file and include it in all the pages.