What are allowed characters in cookies?

前端 未结 13 1297
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 03:36

What are the allowed characters in both cookie name and value? Are they same as URL or some common subset?

Reason I\'m asking is that I\'ve recently hit some strange

13条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 04:27

    In ASP.Net you can use System.Web.HttpUtility to safely encode the cookie value before writing to the cookie and convert it back to its original form on reading it out.

    // Encode
    HttpUtility.UrlEncode(cookieData);
    
    // Decode
    HttpUtility.UrlDecode(encodedCookieData);
    

    This will stop ampersands and equals signs spliting a value into a bunch of name/value pairs as it is written to a cookie.

提交回复
热议问题