如何使用具有相同名称的多个Cookie

时间:2014-01-09 19:45:12

标签: c# asp.net cookies

类似:

Multiple cookies with same name

How to handle multiple cookies with the same name?


我在asp控件的页面加载上设置了一个cookie。我有一个函数获取cookie并将其存储在var中以供使用。我的问题是,在阅读了关于cookie的msdn网站之后,我应该从Request还是Response获取cookie,然后设置另一个的值?或者我应该从Response获取cookie并设置它的值?最后一个似乎不太对,但我不知道。

MSDN文字:

The browser is responsible for managing cookies on a user system. Cookies are sent to the browser via the HttpResponse object that exposes a collection called Cookies. You can access the HttpResponse object as the Response property of your Page class. Any cookies that you want to send to the browser must be added to this collection. When creating a cookie, you specify a Name and Value. Each cookie must have a unique name so that it can be identified later when reading it from the browser. Because cookies are stored by name, naming two cookies the same will cause one to be overwritten.

我认为我应该从request获取它并设置response cookie,这似乎不太正确,因为我最终得到了具有不同值的双cookie。我担心的cookie是TRACKINGINFO。它设置在不同的子域上,我需要获取值,只需添加它并重新设置值。

以下是Google Chrome的请求标题:

ASP.NET_SessionId=bi2ingjjk5f4nhm; referer_domain=dev-znode.local; referer_query=; TRACKINGINFO=1234%2C526%2C1234%2C526; __utma=251778844.1427755012.1389292549.1389292549.1389292549.1; __utmb=251778844.16.10.1389292549; __utmc=251778844; __utmz=251778844.1389292549.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); ZNodeCookie; LocaleID=43; TRACKINGINFO=1234%2C526%2C1234%2C526,2508

请求标题:

TRACKINGINFO=1234%2C526%2C1234%2C526,2500; expires=Fri, 10-Jan-2014 19:21:53 GMT; path=/

1 个答案:

答案 0 :(得分:0)

是的,您应该从请求中获取Cookie并将其设置为响应。

HttpResponse中有魔术代码可以克隆要求的新cookie(在您的情况下可能会以某种方式进行请求),因此开发人员可以随机选择从服务器上读取cookie的位置。

在请求生命周期的早期(在响应中设置任何cookie之前)读取cookie可能更清晰,并且在响应时间设置cookie(但不能太迟,因为它必须在响应主体的第一部分刷新到客户端之前发生)。