Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between HttpResponse: SetCookie, AppendCookie, Cookies.Add

there are some different ways to create multi value cookies in ASP.NET:

var cookie = new HttpCookie("MyCookie"); cookie["Information 1"] = "value 1"; cookie["Information 2"] = "value 2";  // first way Response.Cookies.Add(cookie);  // second way Response.AppendCookie(cookie);  // third way Response.SetCookie(cookie); 

When should I use which way? I've read that SetCookie method updates the cookie, if it already exits. Doesn't the other ways update the existing cookie as well?

And is the following code best practice for writing single value cookies?

Response.Cookies["MyCookie"].Value = "value";

like image 992
Felix C Avatar asked Apr 08 '13 21:04

Felix C


People also ask

What is the difference between request cookies and response cookies?

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.

What is Aspnet cookie?

ASP.NET Cookie is a small bit of text that is used to store user-specific information. This information can be read by the web application whenever user visits the site. When a user requests for a web page, web server sends not just a page, but also a cookie containing the date and time.

What is the proper syntax to set the cookie named my cookie to the value of my value?

Cookies["MyCookie"]. Value = myValue; Response.

What is cookies in C sharp?

Cookies is a small piece of information stored on the client machine. This file is located on client machines "C:\Document and Settings\Currently_Login user\Cookie" path. Its is used to store user preference information like Username, Password,City and PhoneNo etc on client machines.


1 Answers

If I remember correctly both

Response.Cookies.Add(..) 

and

Response.AppendCookie(..) 

will allow multiple cookies of the same name to be appended to the response.

On the other hand

Response.SetCookie(..) 

and

Response.Cookies[key].Value = value; 

will always overwrite previous cookies of the same name.

like image 187
Wiktor Zychla Avatar answered Sep 26 '22 01:09

Wiktor Zychla