zoukankan      html  css  js  c++  java
  • ASP.NET之Cookie(坑爹的Response.Cookies.Remove)

    在web开发中Cookie是必不可少的

    .NET自然也有一个强大的Cookie操作类,我们用起来也非常方便,不过在使用中我们会发现一个坑爹的事情Response.Cookies.Remove删除不了Cookie。

    例子如下:

    [csharp] view plaincopy
     
    1. protected void Page_Load(object sender, EventArgs e)  
    2. {  
    3.     if (!IsPostBack)  
    4.     {  
    5.         HttpCookie UserInfo = new HttpCookie("UserInfo");  
    6.         UserInfo.Value = "bdstjk";  
    7.         Response.Cookies.Add(UserInfo);  
    8.     }  
    9.   
    10. }  
    11.   
    12. protected void btnRemoveCookie_Click(object sender, EventArgs e)  
    13. {  
    14.     Response.Cookies.Remove("UserInfo");  
    15.     Response.Write("<script type="text/javascript">alert("删除Cookie成功!");</script>");  
    16. }  
    17.   
    18. protected void btnCheckCookie_Click(object sender, EventArgs e)  
    19. {  
    20.     if (Request.Cookies["UserInfo"] != null)  
    21.     {  
    22.         Response.Write("Cookie存在,"+Request.Cookies["UserInfo"].Value);  
    23.     }  
    24.     else  
    25.     {  
    26.         Response.Write("Cookie不存在");  
    27.     }  
    28. }  

     

    页面代码:

    [csharp] view plaincopy
     
    1. <asp:Button ID="btnRemoveCookie" runat="server" Text="删除Cookie"   
    2.             onclick="btnRemoveCookie_Click" />  
    3. <asp:Button ID="btnCheckCookie" runat="server" Text="检查Cookie"   
    4.             onclick="btnCheckCookie_Click" />  

    运行代码测试,你会发现,怎么点删除按钮,cookie都存在,如下图:

    这是为什么呢?明明是执行了删除cookie的操作,为什么就是删不掉呢?

    我们去看看.NET的HttpCookieCollection实现源码

    [csharp] view plaincopy
     
    1. public void Remove(string name)  
    2. {  
    3.     if (this._response != null)  
    4.     {  
    5.         this._response.BeforeCookieCollectionChange();  
    6.     }  
    7.     this.RemoveCookie(name);  
    8.     if (this._response != null)  
    9.     {  
    10.         this._response.OnCookieCollectionChange();  
    11.     }  
    12. }  
    13.   
    14.    

    这个操作在HttpCookieCollection这个集合里面删除了cookie,当服务器将数据传输到客户端的时候,不会包含这个已经在服务端删除了的Cookie的任何信息,浏览器也就不会对它做任何改变(remove方法只是不让服务器向客户机发送那个被删除的cookie,与此cookie留不留在客户机里无关)。所以cookie删除不掉的情况就出现。

    那么如果我们想删除cookie应该怎么做呢?

    把删除cookie的代码改成如下语句:

    [csharp] view plaincopy
     
    1. if (Request.Cookies["UserInfo"] != null)  
    2. {  
    3.     Response.Cookies["UserInfo"].Expires = DateTime.Now.AddDays(-1);  
    4. }  
    5. Response.Write("<script type="text/javascript">alert("删除Cookie成功!");</script>");  

    我们再运行程序,测试:

    好了。Cookie已经删除。通过设置Cookie的过期时间为负,强制使Cookie过期。就能实现我们需要的效果了。

    既然Response.Cookies.Remove没有办法实现我们需要的效果,为什么微软还有留着呢,因为CookieCollection实现ICollection接口,romove是必须实现的方法,尽管它没多大的实际价值。而集合的romove也应该是这样的实现方式,只不过微软在写MSDN的时候,描述得太不清楚了,给我们造成了不小的麻烦。

  • 相关阅读:
    阿凯
    hlg神秘植物--矩阵快速幂
    poj1185炮兵阵地--状态dp
    该怎么办
    hlg2096---状态压缩dp
    这个世界

    第三篇
    LCT板子
    spoj COT
  • 原文地址:https://www.cnblogs.com/gc2013/p/4494401.html
Copyright © 2011-2022 走看看