如何在asp.net c#
中从浏览器中删除cookie答案 0 :(得分:19)
这是怎么做的。
if (Request.Cookies["MyCookie"] != null)
{
HttpCookie myCookie = new HttpCookie("MyCookie");
myCookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(myCookie);
}
答案 1 :(得分:7)
以下是您可以删除所有Cookie的代码:
void Page_Load()
{
string[] cookies = Request.Cookies.AllKeys;
foreach (string cookie in cookies)
{
BulletedList1.Items.Add("Deleting " + cookie);
Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
}
}
有关Cookie的更多详细信息:http://msdn.microsoft.com/en-us/library/ms178194.aspx
答案 2 :(得分:2)
基于http://msdn.microsoft.com/en-us/library/ms178195.aspx的助手:
public static void DeleteCookie(
HttpRequest request, HttpResponse response, string name)
{
if (request.Cookies[name] == null) return;
var cookie = new HttpCookie(name) {Expires = DateTime.Now.AddDays(-1d)};
response.Cookies.Add(cookie);
}
答案 3 :(得分:0)
删除Cookie的最简单方法是将过期日期设置为过去的时间
例如,
Set-Cookie: cookieName=; expires=Wed, 12 May 2010 06:33:04 GMT;
它有效,因为在我发布时,Wed, 12 May 2010 06:33:04 GMT
是http时间戳,它永远不会再发生。