Click here to Skip to main content
14,332,477 members
Rate this:
Please Sign up or sign in to vote.
See more:
I m facing problem as follows:
after login when user closes the website by closing the browser tab and not by logging out.
the session is maintained and i m trying to log in again i m getting the error.

i want to clear the session value maintained in cookies after closing the tab directly .
is der any way to do so
Posted
Updated 8-May-17 22:28pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

It can be done using JQuery. Take a look here
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

You can delete cookies using javascript. And if you attach this to the browser close event, it will work.

<body onunload="deleteAllCookies()">


function deleteAllCookies() {
    var cookies = document.cookie.split(";");

    for (var i = 0; i < cookies.length; i++) {
        var cookie = cookies[i];
        var eqPos = cookie.indexOf("=");
        var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
        document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
    }
}


This will clear all cookies onUnload, but cookies should be set using http-only. (read about xss and securing cookies : Protecting Your Cookies: HttpOnly[^])

And if your cookies are well protected, they can't be deleted using javascript.

A solution is to create a javascript function which call an aspx-page which clear the session and remove all cookies.

window.onbeforeunload = function(){
   $.get('removeSession.aspx', function() {
      // all good
   });
}

In your removeSession.aspx you can do your stuff in page_load


protected void Page_Load(object sender, EventArgs e)
{
    Session.Abandon();
    foreach (string cookie in Request.Cookies)
    {
        Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
    }
}
   
v3

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100