Failed to update cookies in asp.net mvc

I can write and read cookies, but I cannot change the value for an existing cookie, it always has the first value set. I found several ways how this can be implemented, but no one works. Here is my code:

private void AddPost(string key) { var context = System.Web.HttpContext.Current; var request = context.Request; var response = context.Response; var cookie = request.Cookies[Constants.PostsViewing]; if (cookie == null || string.IsNullOrEmpty(cookie.Value)) { response.Cookies.Add(new HttpCookie(Constants.PostsViewing, key) { Expires = DateTime.Now.AddDays(365) }); } else { if (cookie.Value.Split(';').Contains(key)) { return; } var v = cookie.Value + ";" + key; cookie.Value = v; cookie.Expires = DateTime.Now.AddDays(365); response.Cookies.Add(cookie); // this way also doesn't work //cookie.Value = v; //response.AppendCookie(cookie); // and this //response.Cookies[Constants.PostsViewing].Value = v; //response.Cookies[Constants.PostsViewing].Expires = DateTime.Now.AddDays(365); } } 

According to msdn, the cookie should be overwritten.

Each cookie must have a unique name so that it can be identified later by reading it in a browser. Since cookies are stored by name, naming two cookies in the same way will result in overwriting.

Do you have an idea how to fix it?

+6
source share
3 answers

I just came across this exact scenario with a similar block of code:

 public ActionResult Index(int requestValue) { var name = "testCookie"; var oldVal = Request.Cookies[name] != null ? Request.Cookies[name].Value : null; var val = (!String.IsNullOrWhiteSpace(oldVal) ? oldVal + ";" : null) + requestValue.ToString(); var cookie = new HttpCookie(name, val) { HttpOnly = false, Secure = false, Expires = DateTime.Now.AddHours(1) }; HttpContext.Response.Cookies.Set(cookie); return Content("Cookie set."); } 

The first time you run the cookie, it will be set without incident. But any subsequent run will never update it at all (value or expiration).

It turns out that the semicolon is an illegal character in the cookie value, and trying to differentiate your values ​​with this will truncate the cookie value. If we change the half-deck to another character, like a pipe (|), everything will be fine.

Consider the header sent for the cookie value (courtesy of Fiddler):

The response sent 61 bytes of cookie data:

Set-Cookie: testCookie = 2; 1; expires = Tue, 09-Sep-2014 19:23:43 GMT; Path = /

As we can see, a colon is used to separate parts of the cookie definition. Thus, if you want to use the half-line in the cookie itself, it must be encoded so as not to be misinterpreted. This answer gives a more detailed overview of the actual specification: fooobar.com/questions/28819 / ....

+10
source

You cannot use half-time as plain text as a separator.

According to the ancient Netscape cookie_spec:

This line is a sequence of characters excluding comma, comma and space.

+4
source

You cannot modify a cookie directly. Instead, you create a new cookie to override the old one. http://msdn.microsoft.com/en-us/library/vstudio/ms178194(v=vs.100).aspx

Try

 var v = cookie.Value + ";" + key; Response.Cookies[Constants.PostsViewing].Value = v; Response.Cookies[Constants.PostsViewing].Expires = DateTime.Now.AddDays(365); 

This should change the client response instead of server requests.

To use Response.AppendCookie, you first need to get an HttpCookie from your Cookies collection.

+1
source

Source: https://habr.com/ru/post/974919/


All Articles