Problem with deleting cookies

M

Michal A. Valasek

Hello,

I have problem deleting cookies and cookies values (using framework version
1.1 on W2003).

When I try to remove entire cookie, by calling
Response.Cookies.Remove("Test"), nothing happens, the cookie "Test" remains
unchanged.

When I try to remove one of cookie values, by calling
Response.Cookies("Test").Values.Remove(Key), all values are cleared (but
cookie itself remains), regardless what is used as Key - existing value
name, non-existent value name, empty string.

It seems to me as error in framework, because documentation says that it
should work. Or it's problem between keyboard and chair?
 
P

Peter O'Reilly

No bug with the Framework, although as you have learned the Remove method is
a bit confusing to have for the Cookie collection. (Maybe this is because
the cookie collection inherits from some other collection and yes, I'm too
lazy to look :).

Anyway, the solution to your problem is to set the expiration date of the
cookie to some value in the past. Actually once you create a cookie, you
can't access it's expiration date despite it being a read/write property in
the framework. This is due to the official/Non-Microsoft (Netscape/w3c)
cookie specification.

So if you are not confused at this point, the true solution is to create
another cookie in ASP.Net that has the same cookie name as the one you want
to delete and specify an expiration date to some value in the past. The idea
is the old cookie will be replaced by the new one. Alternatively, don't
specify an expiration date is effectively the same thing. Both will delete
the cookie at the end of the user's session.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top