What's the difference in Response.Cookies and Request.Cookies?
I can write to Response.Cookies and I get the cookie but after it expires it's still there.
I tried Request.Cookies.Add and the cookie never shows up.
Printable View
What's the difference in Response.Cookies and Request.Cookies?
I can write to Response.Cookies and I get the cookie but after it expires it's still there.
I tried Request.Cookies.Add and the cookie never shows up.
The cookie collection of the Request is what cookies the client had at the time it connected to the server to request a page.
The cookie collection of the Response is what cookies the server will send to the client. This is where you add cookies.
The browser itself is fully responsible for clearing cookies - some browsers behave differently, however a consistent way to clear a cookie is to specifically set that it was to expire 3 years ago (even if you just issued it during this browser session) - most browsers will then immediately kill that cookie.
I'm writing a cookie:
Why won't Response.Cookies.Remove("mycookie"); work?Code:HttpCookie h = new HttpCookie("mycookie", "cookie value");
h.Expires = new DateTime(2005, 3, 30, 16, 55, 0);
Response.Cookies.Add(h);
I know you have to set it to expire on a past date:
but why have Remove() if it won't work?Code:HttpCookie h = Request.Cookies.Get("mycookie");
h.Expires = new DateTime(2002, 3, 30, 16, 55, 0);
Response.Cookies.Set(h);
Remove only works if you set the cookie in the same response session. So if function A adds the cookie to the Response object, and then three functions later, some code logic decides it needs to be removed, then Remove() works.Quote:
Originally Posted by wey97
It doesn't however remove a cookie added during a previous response. I hear you though, it took me a while to figure that out, and the docs don't clarify that at all.