When to use Request.Cookies through Response.Cookies?

I use the answer when the page event (for example, load), since this is a response from ASP.NET, and request when the button is clicked, since this is a response to ASP.NET for processing? Or is there anything else?

+57
Feb 21 '09 at 23:37
source share
6 answers

These are two different things: one is SAVES [Response], the other is READS [Request]

in Cookie (talking computer science) :) you save a small file for a period of time that contains an object of type string

in the .NET framework you save a cookie :

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 in one minute ... we usually do now.AddMonth (1) so that you can save the cookie for one whole month.

To extract the cookie , you use a request (you request), for example:

 HttpCookie myCookie = new HttpCookie("MyTestCookie"); 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 has already passed, for example now.AddMinutes (-1)

this will delete the cookie.

As you can see, every time a cookie expires, this file is automatically deleted from the system.

+95
Feb 21 '09 at 23:57
source share

In a web application, a request is what comes from the browser, and a response is what the server sends back. When checking cookies or cookies from a browser, you must use Request.Cookies. When you create cookies to be sent to your browser, you need to add them to Response.Cookies.

+38
Feb 21 '09 at 23:45
source share

When writing a cookie, use Response, but the reading may depend on your situation. Usually you read “Request”, but if your application is trying to get a cookie just written or updated, and it didn’t return to the browser in both directions, you may need to read it in the “Answer” form.

I have been using this template for a while and it works well for me.

 public void WriteCookie(string name, string value) { var cookie = new HttpCookie(name, value); HttpContext.Current.Response.Cookies.Set(cookie); } public string ReadCookie(string name) { if (HttpContext.Current.Response.Cookies.AllKeys.Contains(name)) { var cookie = HttpContext.Current.Response.Cookies[name]; return cookie.Value; } if (HttpContext.Current.Request.Cookies.AllKeys.Contains(name)) { var cookie = HttpContext.Current.Request.Cookies[name]; return cookie.Value; } return null; } 
+15
Feb 23 '09 at 0:48
source share

Cookies are sent from the browser to the Request.Cookies collection. This is where you read the cookies that were sent.

To send cookies back to your browser, you put them in the Response.Cookies collection.

If you want to delete a cookie, you must tell the browser to delete it by sending an expired cookie. The browser uses the local time of the client computer, so if you use the server time to create the date, be sure to subtract at least one day to be sure that it actually passed in the local client.

+4
Feb 22 '09 at 0:55
source share

When I create or update cookies in .NET, I usually do this with both a collection of request cookies and responses. Thus, you can be sure that if you try to read the cookie further in the order of the page request, it will have the correct information.

+2
Feb 23 '09 at 1:10
source share

Andrew code provided an error in the "AllKeys.Contains" method. So I corrected a little ..

 public void WriteCookie(string strCookieName, string strCookieValue) { var hcCookie = new HttpCookie(strCookieName, strCookieValue); HttpContext.Current.Response.Cookies.Set(hcCookie); } public string ReadCookie(string strCookieName) { foreach (string strCookie in HttpContext.Current.Response.Cookies.AllKeys) { if (strCookie == strCookieName) { return HttpContext.Current.Response.Cookies[strCookie].Value; } } foreach (string strCookie in HttpContext.Current.Request.Cookies.AllKeys) { if (strCookie == strCookieName) { return HttpContext.Current.Request.Cookies[strCookie].Value; } } return null; } 
+1
Nov 22 '10 at 10:37
source share



All Articles