Broken string in cookie after ampersand (javascript)

断了今生、忘了曾经 提交于 2019-11-29 16:24:13
stephen

I think I have run into a similar problem in an ASP.NET MVC app.

When I save a string containing ampersands to a cookie name/value pair it actually gets split into a series of name/value pairs

e.g. attempting to save ("value","cookiedata123&book=2&page=0") will result in 3 name value pairs being created "value"="cookiedata123"; "book"="2"; and "page"="0".

I have resolved this by URL Encoding the value just before writing to the cookie and URL Decoding it as soon as I read it out. In .net the calls look like this:

// Encode
return System.Web.HttpUtility.UrlEncode(cookieData);

// Decode
return System.Web.HttpUtility.UrlDecode(encodedCookieData);

That takes care of any ampersands, equals signs etc that can cause problems. See this post here for info on characters that are allowed in cookies. Allowed characters in cookies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!