12

When storing a value in a cookie using C#, what is the best way to encode (or escape) the value so that it can be retrieved and decoded/unescaped reliably?

I'm not talking about encryption.

Paul Lucas
  • 1,302
  • 1
  • 12
  • 18
  • Related to [this question](https://stackoverflow.com/questions/1136405/handling-a-comma-inside-a-cookie-value-using-nets-c-system-net-cookie) – Gyrfalcon Feb 18 '19 at 08:33

1 Answers1

17

Well, the safest thing to do is use UrlEncoding (use HttpServerUtility.UrlEncode).

Tom Cabanski
  • 7,828
  • 2
  • 22
  • 25
  • A good explanation of why this is true can be found [here](https://stackoverflow.com/questions/1969232/allowed-characters-in-cookies) – Gyrfalcon Feb 18 '19 at 08:34