Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Request.Cookies over Response.Cookies?

Tags:

c#

asp.net

Do I use response when at a page event (e.g. load) as this is a response from ASP.NET, and request when pressing a button as this is a response going to ASP.NET for processing? Or is there more to it?

like image 496
GurdeepS Avatar asked Feb 21 '09 23:02

GurdeepS


People also ask

Are cookies sent automatically with every request?

No. Not every request sends the cookies. It depends on the cookie configuration and client-server connection. For example, if your cookie's secure option is set to true then it must be transmitted over a secure HTTPS connection.

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

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

Can a get request set cookies?

Also I would say it is perfectly acceptable to change or set a cookie in response for the GET request because you just return some data.

How do cookies work HTTP?

HTTP cookies, or internet cookies, are built specifically for Internet web browsers to track, personalize, and save information about each user's session. A “session” just refers to the time you spend on a site. Cookies are created to identify you when you visit a new website.


2 Answers

They are 2 different things, one SAVES [Response], the other READS [Request]

in a Cookie (informatics speaking) :) you save a small file for a period of time that contains an object of the type string

in the .NET framework you save a cookie doing:

HttpCookie myCookie = new HttpCookie("MyTestCookie"); DateTime now = DateTime.Now;  // Set the cookie value. myCookie.Value = now.ToString(); // Set the cookie expiration date. myCookie.Expires = now.AddMinutes(1);  // Add the cookie. Response.Cookies.Add(myCookie);  Response.Write("<p> The cookie has been written."); 

You wrote a cookie that will be available for one minute... normally we do now.AddMonth(1) so you can save a cookie for one entire month.

To retrieve a cookie, you use the Request (you are Requesting), like:

HttpCookie myCookie = Request.Cookies["MyTestCookie"];  // Read the cookie information and display it. if (myCookie != null)    Response.Write("<p>"+ myCookie.Name + "<p>"+ myCookie.Value); else    Response.Write("not found"); 

Remember:

To Delete a Cookie, there is no direct code, the trick is to Save the same Cookie Name with an Expiration date that already passed, for example, now.AddMinutes(-1)

this will delete the cookie.

As you can see, every time that the time of life of the cookie expires, that file is deleted from the system automatically.

like image 124
balexandre Avatar answered Sep 28 '22 02:09

balexandre


In a web application the request is what comes from the browser and the response is what the server sends back. When validating cookies or cookie data from the browser you should use the Request.Cookies. When you are constructing cookies to be sent to the browser you need to add them to Response.Cookies.

like image 20
tvanfosson Avatar answered Sep 28 '22 01:09

tvanfosson