Click here to Skip to main content
15,937,602 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
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
Updated 8-May-17 22:28pm

It can be done using JQuery. Take a look here
Share this answer
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)
    foreach (string cookie in Request.Cookies)
        Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
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