清除cookie可用下面2种的方法。注意如果要清除的cookie的域是指定的,则这里也应该指定,否则无效
1:
Response.Cookies["mycookiename"].Expires = DateTime.Now.AddDays(-1);
Response.Cookies["mycookiename"].Domain = ".test.com";
2:
HttpCookie cookies = new HttpCookie("mycookiename", null);
cookies.Domain = ".test.com";
cookies.Expires = new DateTime(1900, 1, 1);
Response.Cookies.Add(cookies);
3:
HttpCookie us = Request.Cookies["user"];
if (us != null)
{
//会让指定有效期的cookie变成会话cookie,要等关闭浏览器该cookie才消失
us.Values.Remove("username");
Response.AppendCookie(us);
}
4、
HttpCookie cookie = new HttpCookie("mycookiename2", cookievalue);
cookie.HttpOnly = true; //设置后客户端能看到,js无法访问到,js也无法修改名为mycookiename的值
cookie.Domain = ".test.com";
Response.Cookies.Add(cookie);