首页 新闻 会员 周边 捐助

C#关于Cookie删除问题

0
[已关闭问题] 关闭于 2018-05-29 18:25

代码如下:

复制代码
    private void ValidCookies()
        {
            int count =Request.Cookies.Count;
            if (count > 5) {
                HttpCookieCollection cookies = Request.Cookies;
                for (int i = 0; i < cookies.Count; i++)
                {
                    Response.Write("<br/>Cookie键:" + cookies[i].Name + ",值:" + cookies[i].Value + ",Domain:" + cookies[i].Domain + ",Path:" + cookies[i].Path + "Expire:" + cookies[i].Expires.ToString("yyyy-MM-dd HH:mm:ss"));
                }
                return;
            }
           
            if (!string.IsNullOrEmpty(Request.Cookies["xfk_sid"].Value))
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies["xfk_sid"];
                if (cookie != null)
                {
                    cookie.Expires = DateTime.Now.AddYears(-3);
                    HttpContext.Current.Response.Cookies.Add(cookie);
                }
                ValidCookies();
            }
        }
复制代码

执行这个方法,为什么cookie没删掉,却越来越多呢?

结果如下:

Cookie键:xfk_sid,值:sadasdsadsadsadsa,Domain:,Path:/Expire:2015-05-29 18:12:59
Cookie键:xfk_sid,值:sadasdsadsadsadsa,Domain:,Path:/Expire:2015-05-29 18:12:59
Cookie键:xfk_sid,值:sadasdsadsadsadsa,Domain:,Path:/Expire:2015-05-29 18:12:59
Cookie键:xfk_sid,值:sadasdsadsadsadsa,Domain:,Path:/Expire:2015-05-29 18:12:59
Cookie键:xfk_sid,值:sadasdsadsadsadsa,Domain:,Path:/Expire:2015-05-29 18:12:59
Cookie键:xfk_sid,值:sadasdsadsadsadsa,Domain:,Path:/Expire:2015-05-29 18:12:59

skybirdzw的主页 skybirdzw | 初学一级 | 园豆:3
提问于:2018-05-29 18:18
< >
分享
所有回答(1)
0

Response之后在下次请求时才会生效,Response不会影响当前Request,相关博问:asp.net core中的cookie在同一个方法体里面删除后为什么还能获取到值?

dudu | 园豆:30778 (高人七级) | 2018-05-29 18:27

明白了,感谢!

支持(0) 反对(0) skybirdzw | 园豆:3 (初学一级) | 2018-05-31 11:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册