代码如下:
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