cookie 怎么清除呢
我在网站保存用户信息时,用的是cookie保存的, 做“退出” 功能时,想在后台清空cookie.
可是我试了这几个都没用,要怎样做呢?
在登录页面:
保存姓名
HttpCookie cookie = new HttpCookie("studentName", userName);
Response.Cookies.Add(cookie);
在登出页面:
想清空cookie
for (int i = 0; i < Request.Cookies.Count; i++)
{
Request.Cookies.Clear();
}
foreach (HttpCookie cookie in Request.Cookies)
{
cookie.Expires = DateTime.Now.AddDays(-1);
HttpContext.Current.Response.Cookies.Add(cookie);
}
HttpCookie aCookie;
string cookieName;
int limit = Request.Cookies.Count;
for (int i = 0; i < limit; i++)
{
cookieName = Request.Cookies[i].Name;
aCookie = new HttpCookie(cookieName);
aCookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(aCookie);
}
这三种都没有用,怎么回事呢?
------解决方案--------------------Response.Cookies["name"].Value = "username";//赋值
Response.Cookies["name"].Value = "";//清空