1

I would like to set the value of mycookie to value=somevalue, thus:

cookie view in chome

The recommended approach of using Cookies.Append does not work, as the value is URL encoded and ends up being value%3Dsomevalue.

My code for adding the cookie:

_context.Response.Cookies.Append("mycookie", $"value=somevalue");

My code for reading the cookie

_context.Request.Cookies["mycookie"]

The question: what would be the best way to bypass the URL encoding and set the raw value of the cookie?

Dawid O
  • 6,091
  • 7
  • 28
  • 36
  • How about creating the cookie using `HttpCookie MyCookie = new HttpCookie("mycookie");` and then setting the value as `MyCookie.Value = somevalue` and then adding it as `context.Response.Cookies.Add(MyCookie);` – Gopesh Sharma Jul 01 '19 at 09:40
  • `HttpCookie` used to part of the `System.Web` for the old ASP.NET. I am not aware of any `HttpCookie` class in ASP.NET Core. Using the old way would require access to another `HttpContext` which I would prefer to avoid: https://stackoverflow.com/questions/38346333/why-i-cant-use-httpcontext-or-httpcookie-asp-net-core-1-0. – Dawid O Jul 01 '19 at 09:46
  • Then how about not giving the `value=` itself, just try _context.Response.Cookies.Append("mycookie", "somevalue"); – Gopesh Sharma Jul 01 '19 at 09:54
  • This works fine, but I do need the `value=` in there. I just don't want the `=` to be URL encoded. – Dawid O Jul 01 '19 at 09:59

1 Answers1

1

I found a reasonable workaround, by setting the cookie header manually.

_context.Response.Headers.Append("Set-Cookie", "mycookie=value=somevalue");

You can use the same code to get the cookie back:

_context.Request.Cookies["mycookie"]
Dawid O
  • 6,091
  • 7
  • 28
  • 36