Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I delete this cookie?

Okay, here is the 411 - I have the following event handler in my Global.asax.cs file:

private void Global_PostRequestHandlerExecute(object sender, EventArgs e)
{
   if (/* logic that determines that this is an ajax call */)
   {
      // we want to set a cookie
      Response.Cookies.Add(new HttpCookie("MyCookie", "true"));
   }
}

That handler will run during Ajax requests (as a result of the Ajax framework I am using), as well as at other times - the condition of the if statement filters out non-Ajax events, and works just fine (it isn't relevant here, so I didn't include it for brevity's sake).

It suffices us to say that this works just fine - the cookie is set, I am able to read it on the client, and all is well up to that point.

Now for the part that drives me nuts.

Here is the JavaScript function I am using to delete the cookie:

function deleteCookie(name) {
   var cookieDate = new Date();
   cookieDate.setTime(cookieDate.getTime() - 1);
   document.cookie = (name + "=; expires=" + cookieDate.toGMTString());
}

So, of course, at some point after the cookie is set, I delete it like so:

deleteCookie("MyCookie");

Only, that doesn't do the job; the cookie still exists. So, anyone know why?

like image 742
Jason Bunting Avatar asked Sep 11 '08 05:09

Jason Bunting


People also ask

Why do some cookies not delete?

When trying to clear Cookies after a scan, You receive a notification that Cookies have been unable to be cleaned. This is due to background processes for the browser running in the background during the scan.

How do I get rid of cookies that won't delete on safari?

To clear your cookies and keep your history, go to Settings > Safari > Advanced > Website Data, then tap Remove All Website Data.


2 Answers

you have to delete your cookie at the same path where you created it. so create your cookie with path=/ and delte it with path=/ as well..

like image 84
Andreas Petersson Avatar answered Oct 12 '22 15:10

Andreas Petersson


  • Have you checked the client-side and server-side cookie domains and paths to ensure they're the same?
  • Is one cookie secure and the other not?
  • Other than that, I would suspect server/client clock sync issues, as Erlend suggests.
like image 43
Robert J. Walker Avatar answered Oct 12 '22 15:10

Robert J. Walker