Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys,

I want to delete cookies from my application when user signout from his account,
is it possible?
if yes please suggest me the way how can i do this?




thanks in adv.
Posted

Cookies are located on the client machine.
If the user ends their session from the client side you can try using the following javascript.
If the user simply closes their browser, or if the browser closes unexpectedly, I'm not sure the event will fire.
You won't be able to do this effectively from the server side code from a Response because that would require a postback which does not happen if the user navigates away from your page or closes their browser. If you only want to remove the cookies on explicit sign-out, then you can use the other answer from Prasad(Solution 1) which shows how you can do it from the server side.
JavaScript
window.onbeforeunload = function() {
   var cookies = document.cookie.split(";");
   for (var i = 0; i < cookies.length; i++)
      eraseCookie(cookies[i].split("=")[0]);
};

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

Code Reference[^]
 
Share this answer
 
v4
Comments
Espen Harlinn 6-Sep-12 10:17am    
5'ed!
fjdiewornncalwe 6-Sep-12 10:28am    
Thanks Espen
Firstly have a look on this MSDN[^] article.

Try this:
C#
if (Request.Cookies["userId"] != null)
    {
        Response.Cookies["userId"].Expires = DateTime.Now.AddDays(-1);   
    }

But it also makes sense to use
C#
Session.Abandon();

Found it here[^]

Refer: Delete cookies on sign out[^]
 
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