之间HttpContext.Request.Cookies和HttpContext.Response.Cookies关系 [英] Relationship between HttpContext.Request.Cookies and HttpContext.Response.Cookies

查看:195
本文介绍了之间HttpContext.Request.Cookies和HttpContext.Response.Cookies关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直与code实验,将清除所有的cookie在 HttpContext.Response

起初,我用这个:

 的DateTime cookieExpires = DateTime.Now.AddDays(-1);的for(int i = 0; I< HttpContext.Request.Cookies.Count;我++)
{
    HttpContext.Response.Cookies.Add(
        新的HttpCookie(HttpContext.Request.Cookies [I] .name和NULL){过期= cookieExpires});
}

不过,这与 OutOfMemoryException异常因为循环永远不会退出错误 - 每次添加一个cookie在响应,它也被添加到`请求。

下面的方法如下:

 的DateTime cookieExpires = DateTime.Now.AddDays(-1);清单<串GT; cookieNames =新的List<串GT;();的for(int i = 0; I< HttpContext.Request.Cookies.Count;我++)
{
    cookieNames.Add(HttpContext.Request.Cookies [I] .Name点);
}的foreach(在cookieNames串cookieName)
{
    HttpContext.Response.Cookies.Add(
       新的HttpCookie(cookieName,NULL){过期= cookieExpires});
}

那么,究竟是什么关系 HttpContext.Request.Cookies HttpContext.Response.Cookies <? / p>

解决方案

Request.Cookies时包含一套完整的饼干,这些都该浏览器向服务器发送那些您只需在服务器上创建的。

<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.htt$p$psponse.cookies.aspx\"><$c$c>Response.Cookies包含服务器将发回的cookie。结果
这个系列开始是空的,应该改为修改浏览器的Cookie。

该文件规定:


  

ASP.NET包括两个内在的cookie
  集合。收集访问
  通过Cookies集合
  HTT prequest包含饼干
  由客户端所发送的
  服务器中的Cookie头。该
  收集通过访问
  Htt的presponse的Cookies集合
  包含关于创造了新的饼干
  服务器和发送到客户
  在Set-Cookie头。


  
  

您后,使用添加一个cookie
  HTT presponse.Cookies收藏,
  Cookie是在立即可
  HTT prequest.Cookies收集,甚至
  如果响应没有被发送到
  客户端



如果您在运行循环向后你的第一个code样品应该工作。结果
新的饼干会结束后添加,所以向后循环会忽略它们。

I have been experimenting with code that will clear all of the cookies in an HttpContext.Response.

Initially, I used this:

DateTime cookieExpires = DateTime.Now.AddDays(-1);

for (int i = 0; i < HttpContext.Request.Cookies.Count; i++)
{
    HttpContext.Response.Cookies.Add(
        new HttpCookie(HttpContext.Request.Cookies[i].Name, null) { Expires = cookieExpires });
}

However, this will error with an OutOfMemoryException because the for loop never exits - each time you add a cookie to the Response, it also gets added to the `Request.

The following approach works:

DateTime cookieExpires = DateTime.Now.AddDays(-1);

List<string> cookieNames = new List<string>();

for (int i = 0; i < HttpContext.Request.Cookies.Count; i++)
{
    cookieNames.Add(HttpContext.Request.Cookies[i].Name);
}

foreach (string cookieName in cookieNames)
{
    HttpContext.Response.Cookies.Add(
       new HttpCookie(cookieName, null) { Expires = cookieExpires });
}

So, what exactly is the relationship between HttpContext.Request.Cookies and HttpContext.Response.Cookies?

解决方案

Request.Cookies contains the complete set of cookies, both those that browser send to the server and those that you just created on the server.

Response.Cookies contains the cookies that the server will send back.
This collection starts out empty and should be changed to modify the browser's cookies.

The documentation states:

ASP.NET includes two intrinsic cookie collections. The collection accessed through the Cookies collection of HttpRequest contains cookies transmitted by the client to the server in the Cookie header. The collection accessed through the Cookies collection of HttpResponse contains new cookies created on the server and transmitted to the client in the Set-Cookie header.

After you add a cookie by using the HttpResponse.Cookies collection, the cookie is immediately available in the HttpRequest.Cookies collection, even if the response has not been sent to the client.


Your first code sample should work if you make the for loop run backwards.
The new cookies will be added after the end, so the backwards loop would ignore them.

这篇关于之间HttpContext.Request.Cookies和HttpContext.Response.Cookies关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆