Delete cookie issue in C#
I'm trying to delete a cookie, however it doesn't get deleted. Here is the code I try to use.
if (Request.Cookies["dcart"] != null)
{
Response.Write(Request.Cookies["dcart"].Expires);
// Response 1/1/0001 12:00:00 AM
Response.Write(Request.Cookies["dcart"].Value);
// Response 229884
HttpCookie myCookie = new HttpCookie("dcart");
myCookie.Expires = DateTime.Now.AddDays(-1d);
myCookie.Value = "";
Response.Cookies.Add(myCookie);
}
Response.Write(Request.Cookies["dcart"].Expires);
// Response 1/1/0001 12:00:00 AM
Response.Write(Request.Cookies["dcart"].Value);
// Response 229884
When I retrieve the cookie again, nothing changes. I check w/ FireFox and Chrome same behavior. Interesting point is that expiration date shows correctly on the browsers but on the code.
I tried followings and didn't work.
PS. The code won't work directly on your machine, because you don't have the cookie.
SOLVED
Problem was the path. The cookie I request was under "/store" path and the one I response path information to "/".
if (Request.Cookies["dcart"] != null)
{
HttpCookie myCookie = new HttpCookie("dcart");
myCookie.Expires = DateTime.Now.AddDays(-1d);
myCookie.Path = "/store";
Response.Cookies.Add(myCookie);
}
When I added path information, it's deleted.
Note: I used Firebug to trace the cookie path.
Cookies are trickier than they look. Read this for a clear description of how to use cookies:
On The Care and Handling of Cookies
To delete a cookie, according to the linked article you need to simply set the expiration time to any time in the past, but do not use DateTime.MinValue because some browsers do not handle that date correctly:
If you want to delete the cookie on the client machine, do not use the obvious Response.Cookies.Remove("MyCookie") which simply tells the cookie not to overwrite the client's cookie (see below for a more detailed explanation), set the cookie's Expires property to any time prior to the current time. This will tell the client to overwrite the current cookie with an expired cookie and the client will never send it back to the server.
Again, the temptation is to use DateTime.MinValue (01-Jan-0001 00:00:00) to delete a cookie; again, this would be a mistake. This time, Netscape 7 will work as expected but Internet Explorer 6 considers this to be an exceptional case. If Internet Explorer receives a cookie with what it considers to be a "blank" expiry date, it will retain the cookie until the browser is closed and then expire it.
The safest (and most symmetric) way to delete the cookie by using an Expiry date of DateTime.Now.AddYears(-30).
ie the correct method is
Request.Cookies["dcart"].Expires = DateTime.Now.AddYears(-30);
This link will provide some answers for you:
https://msdn.microsoft.com/en-us/library/ms178195.aspx
In my case, just worked in localhost, but, when i deployed to the server the problem persisted...
In HttpCookie
, you can try set the Expire parameter DateTime.Now.AddDays(-1d)
, (Set the Expire date to yesterday) and set the property Secure = false
.
After this, worked for me.
链接地址: http://www.djcxy.com/p/11538.html上一篇: 在iOS中本地化数字
下一篇: 在C#中删除cookie问题