Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relationship between HttpContext.Request.Cookies and HttpContext.Response.Cookies

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?

like image 929
Richard Ev Avatar asked Nov 23 '10 16:11

Richard Ev


People also ask

What is the difference between request cookies and response cookies?

Response cookies are cookies that you want the browser to set on the client machine. Request cookies are cookies that already exist on the client side and have been sent by the browser together with the request.

How do I check cookie responses?

In the developer tab related to the cookie, you can view only the cookie related to the response. In order to understand which headers was sent during the request, you have to move under the network tab and then you can click over your request. Here you can now see the headers related to your request. Save this answer.

How can a cookie be added to an HTTP response?

To add a new cookie, use HttpServletResponse. addCookie(Cookie). The Cookie is pretty much a key value pair taking a name and value as strings on construction.

How do you get a response cookie in Python?

How do I get cookie data in python? Use the make_response() function to get the response object from the return value of the view function. After that, the cookie is stored using the set_cookie() function of the response object. It is easy to read back cookies.


1 Answers

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.

like image 84
SLaks Avatar answered Nov 15 '22 10:11

SLaks