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()?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
There is a difference:
Response.Cookies.Add()will allow duplicate cookies to be set http://msdn.microsoft.com/en-us/library/system.web.httpcookiecollection.add.aspxResponse.Cookies.Set()will make sure the cookie is unique by first checking to ensure the cookie doesn’t exist http://msdn.microsoft.com/en-us/library/system.web.httpcookiecollection.set.aspx
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0