HttpCookieCollection.Add vs HttpCookieCollection.Set - Does the Request.Cookies collection get copied to the Response.Cookies collection?
Asked Answered
P

1

24

I just want to clear this up.

I know that if I have set a cookie on a previous request, it will show up in my Request.Cookies collection.

I want to update my existing Cookie.

Are the cookies from my Request.Cookies collection already copied to my Response.Cookies collection? Do I need to add a new cookie with the same key using Response.Cookies.Add(), or do I need to use Response.Cookies.Set()?

Pumphrey answered 1/4, 2011 at 18:23 Comment(0)
Y
30

There is a difference:

Duplicate cookies typically requires extra handling to determine which is the most recent. I'm not sure of a case when you would want duplicate cookies on the same site, maybe someone else can chime in with an example

Edit: In your case, you want to use set because you are updating.

Youngman answered 1/4, 2011 at 18:43 Comment(3)
Thanks - I also did some reflection and I realized I am asking the wrong question. Please check out this post: #5518095Pumphrey
this is badly documented. the original documentation includes the statement "Updates the value of an existing cookie in a cookie collection" which is just wrong because if the cookie does not exist, it is added. the underlying code uses a BaseAdd call which updates existing keys and adds new keys.Radloff
I don't think one can have the confidence that code the document refers to in 2011 is the same code you have linked to that is 4.7.2.Youngman

© 2022 - 2024 — McMap. All rights reserved.