当前位置: 代码迷 >> ASP.NET >> 高分:Cookies.值如何清除不了
  详细解决方案

高分:Cookies.值如何清除不了

热度:1337   发布时间:2013-02-25 00:00:00.0
高分求救:Cookies.值怎么清除不了
protected void Button1_Click(object sender, EventArgs e)
  {
  Response.Cookies["tgdl"].Expires = DateTime.Now.AddHours(-1);
  Response.Cookies["tgdlmz"].Expires = DateTime.Now.AddHours(-1);  
  Response.Cookies.Clear();
 Response.Redirect("hydl.aspx");
  }
这样清除,但Cookies.值还是存在

 protected void Page_Load(object sender, EventArgs e)
  {
  if (Request.Cookies["tgdl"].Value != null && Request.Cookies["tgdlmz"].Value!= null)
  {
  this.Panel1.Visible = false;
  this.Panel2.Visible = true;
  this.Label1.Text = Request.Cookies["tgdlmz"].Value;
  this.HyperLink3.NavigateUrl = Request.Cookies["tgdl"].Value;

  }

  }

------解决方案--------------------------------------------------------
不能直接删除用户计算机中的 Cookie。但是,可以通过将 Cookie 的到期日期设置为过去的日期,让用户的浏览器来删除 Cookie。当用户下一次向设置该 Cookie 的域或路径内的页发出请求时,浏览器将确定该 Cookie 已到期并将其移除。

注意 
调用 Cookies 集合的 Remove 方法可从服务器端的集合中移除 Cookie,使 Cookie 不会被发送至客户端。但是,如果客户端已存在 Cookie,则该方法无法从客户端将其移除。
 

向 Cookie 分配已过去的到期日期

C# 复制代码 
if (Request.Cookies["UserSettings"] != null)
{
HttpCookie myCookie = new HttpCookie("UserSettings");
myCookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(myCookie);
}
 

------解决方案--------------------------------------------------------
this.Request.Cookies["adminusers"].Expires = DateTime.Now.AddHours(-1);
Response.Cookies.Add(Request.Cookies["adminusers"]);
设置过期之后要写回客户端才管用的
  相关解决方案