參考 :?http://www.cnblogs.com/fish-li/archive/2011/07/03/2096903.html
?
上文結尾有提到一個說法
4. HttpRequest.Cookies 與 HttpResponse.Cookies 會有關系(很奇怪吧)。
微軟官網也是這么說的 :?https://msdn.microsoft.com/en-us/library/system.web.httprequest.cookies(v=vs.110).aspx
After you add a cookie by using the?HttpResponse.Cookies?collection, the cookie is immediately available in theHttpRequest.Cookies?collection, even if the response has not been sent to the client.
?
這情況有時候會讓我們有點混淆。所以我特別寫了一篇來解釋它.
首先我們要知道?
1.
var listx = Response.Cookies.AllKeys.ToList(); //empty list if (Response.Cookies["c"] != null) { } var list = Response.Cookies.AllKeys.ToList(); //["c"] got 1 data
即使我們只是讀取,也是再創建?
?
2. httpRequest.cookie 的 expires 永遠是 0001 年
Response.Cookies["c"].Expires.Year == 1
?
回到真題
所以如果說我們想獲取某些請求的cookie,我們可以使用 httpRequest.cookie['a'] 去拿,這時你當然有可能也會把response的一起拿出來, 這時我們需要過濾?
如果cookie.expires.year == 1 就表示他是request 而不是response, 除非你的response 也是year == 1 , ... 這哪里可能=.="?
那么如果我們想要拿一個response則相反,我們去httpResponse拿,然后過濾 year != 1;?
當你的request和response 同時擁有一個同名的cookie時,你使用 request.cookie 和response.cookie 拿出來的cookie是不同的。
說的有點含糊,但是基本上測試一下就明白了。以后有空才寫整齊版本.
?