[转]关于asp.net 中 cookies 的清空
转自:http://www.cnblogs.com/maweidream/archive/2008/10/10/1308011.html
最近做一个项时候登录时用cookies保存了用户id.可是在做退出的时候出现了一点小问题。
![](https://www.cnblogs.com/images/outliningindicators/expandedblockstart.gif)
response.cookies.clear()
![](https://www.cnblogs.com/images/outliningindicators/expandedblockstart.gif)
foreach (httpcookie cookie in response.cookies)
{
cookie.expires = datetime.today.adddays(-5);
response.cookies.remove(cookie);
}
改了之后,一试还是不行,cookies依然还在.最后找了半天,原来是response.cookies.clear()只是清除现在添加到response中的cookies.并不能清除客户端的cookies.改了下代码之后终于可以了
![](https://www.cnblogs.com/images/outliningindicators/expandedblockstart.gif)
foreach (string cookiename in request.cookies.allkeys)
{
httpcookie cookie = request.cookies[cookiename];
cookie.expires = datetime.today.adddays(-5);
response.cookies.add(cookie);
}