42

I use both of these many times in my code and don't really know what the difference is , if a cookie is set shouldn't it be exactly the same in request and response? and is request going to the the most up to date , or response?

EDIT:

ok , I get the difference between a request and a response, but if I type

string a = HttpContext.Current.Request.Cookie["a"].Value;

it is most of the time the same as

string a = HttpContext.Current.Response.Cookie["a"].Value;

but I am wondering what is the difference between using the two.

Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
Scott Selby
  • 9,420
  • 12
  • 57
  • 96

4 Answers4

51

As everyone says Request.Cookies are supposed to be cookies coming from client (browser) and Response.Cookies are cookies that will be send back to client (browser).

There is black magic well documented* code that copies values from Response cookies to Request.Cookies when you add cookies to Response. As result it looks like you have same cookies in both Request and Response. Note that these copied cookies did not come from the client... so beware of making wrong decisions.

Here is a link to discussion about the code: http://forums.asp.net/t/1279490.aspx. In particular, cookies added in the following way will show up in the Request.Cookies collection:

Response.Cookies.Add(HttpCookie("MyCookie", "MyValue"))

*The behavior of cookies getting copied from Response.Cookies is documented in the HttpResponse.Cookies article:

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.

Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • 7
    It sounds more like a bug instead of black magic. Why should this copying happen? – Gabriel Burete Oct 24 '13 at 08:21
  • @GabrielBurete - I've added the same link as in your [original question](http://stackoverflow.com/questions/19557365/response-cookies-show-up-in-request-cookies/19557437#19557437). If you are interested why particular behavior was implemented - I don't know, you may try to ask separate question but generally such archeological questions are closed as non-answerable. – Alexei Levenkov Oct 24 '13 at 16:33
  • 1
    If you replace an existing cookie with a new cookie, `Request.Cookies[NAME]` will only return one of the cookies (empirically, this seems to be the request cookie). However, you'll see both cookies if you enumerate the cookies. – Brian Mar 21 '18 at 20:53
4

The request cookie is what is send from the client to the server (thus what the browser provides). The response cookie are the cookies that you want to place in the browser. The next connection from the browser that accepted the cookie from the response object will provide the cookie in the request object.

Deathspike
  • 8,582
  • 6
  • 44
  • 82
4

The word Response is used in Asp.net to send data from the server to the client and the Request is used to get the data from the client ( in the form of cookies, query string ) etc. Example:

Response.Write("will write the content on the form which will return to the client");
// Response.Cookies will send the cookie to the client browser.
 Response.Cookies.Add(HttpCookie("MyCookie", "MyValue"))
//and Request.Cookies is used to get the cookie value which is already present in the clinet browswer   

and as you mentioned

string a = HttpContext.Current.Request.Cookie["a"].Value;
// I think this will check the cookie which is present in the client browser [ If client has sent the cookie to the server ]

string a = HttpContext.Current.Response.Cookie["a"].Value;
// and this will see the only Response object. If the cookie present in the response object then it will return you otherwise not.
Waqar Janjua
  • 6,113
  • 2
  • 26
  • 36
2

Depends on what context.

Request is the data that gets sent to the server with every http request. Response is the response after the request by the server to the client.

Phillip Schmidt
  • 8,805
  • 3
  • 43
  • 67