I'm saving a cookie in JSON format. In the properties of this object I have links with several parameters. At each link change, I update the cookie with the new value. Example of value saved in cookie:
{"Url":"https://www.site.com.br? utm_source=parceiro&utm_medium=Banner&utm_campaign=campanha","Valor":"https://www.site.com.br"}
When you access the page for the first time, the value saved in the cookie is correct. However, when updating the page the value appears as follows:
{"Url":"https://www.site.com.br?utm_source=parceiro&utm_medium=Banner&utm_medium=Banner&utm_medium=Banner&utm_campaign=campanha&utm_campaign=campanha&utm_campaign=campanha&utm_campaign=campanha","Valor":"https://www.site.com.br"}
Parameters are repeated. With each update the number of parameters increases. I added logs and I verified that the value in cookies is correct, it only appears with several identical parameters when I return the saved value of the cookies. I tested using XML instead of JSON and the same thing happened. Example cookie creation:
public static void SalvarCookie(string nome, string valor, int dias)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[nome];
if (cookie != null)
cookie.Expires = DateTime.Now.AddDays(-1);
cookie = new HttpCookie(nome, valor);
cookie.Expires = DateTime.Now.AddDays(dias);
cookie.HttpOnly = true;
cookie.Secure = true;
HttpContext.Current.Response.Cookies.Add(cookie);
}
Code to convert object to JSON:
public static string ConvertToJson(object valor)
{
return Newtonsoft.Json.JsonConvert.SerializeObject(valor);
}
I tested other forms of cookie update, but all returned the wrong value. In the Chrome inspector the cookie value appears correctly, the problem only appears in the code.
Has anyone ever had this problem?