我使用Cookie在搜索页面中显示某些数据,但是在使用Unicode字符时,我的cookie会使值变得混乱。例如,当我存储Inglês
时,我在阅读时会收到Inglês
。
这就是我保存cookie的方式:
public void SalvaValue(string sessionKey, string sessionValue)
{
Response.Cookies.Add(new HttpCookie(sessionKey));
var httpCookie = Response.Cookies[sessionKey];
if (httpCookie != null) httpCookie.Value = sessionValue;
if (httpCookie != null) httpCookie.Expires = DateTime.Now.AddDays(14);
}
这是我检索它的方式:
if (Request.Cookies["BuscaTipo"] != null)
{
tipoBusca = Request.Cookies["BuscaTipo"].Value.ToString();
var cookie = new HttpCookie("BuscaTipo") { Expires = DateTime.Now.AddDays(-1) };
Response.Cookies.Add(cookie);
}
当我调试网站时,它在设置时在代码中显示正确的值,但在我使用jQuery执行请求后,该值会出现错误的字符。
如何在Cookie中安全存储Unicode字符?
答案 0 :(得分:9)
请参阅How to store other languages (unicode) in cookies and get it back again,Unicode Cookie Value,How to send non-English unicode string using HTTP header?和Allowed characters in cookies,了解您需要对Cookie值进行编码的原因。
简而言之:大多数浏览器都支持标题中的Unicode字符(发送cookie),但不是全部。有些浏览器将Unicode字节解释为ASCII,从而生成Mojibake。
jQuery似乎也根据一些链接问题发挥作用,但我无法重现。
因此,要在所有浏览器中安全地存储Unicode字符(或者更确切地说是任何非ASCII或控制字符),您需要对字符进行编码。这可以通过例如base64和percent-encoding来实现。
后者的实现,略微改编自Cookies and Unicode characters:
public static class CookieExtensions
{
public static string DecodedValue(this HttpCookie cookie)
{
if (cookie == null)
{
throw new ArgumentNullException("cookie");
}
return HttpUtility.UrlDecode(cookie.Value);
}
public static void SetEncodedValue(this HttpCookie cookie, string value)
{
if (cookie == null)
{
throw new ArgumentNullException("cookie");
}
cookie.Value = HttpUtility.UrlEncode(value);
}
public static string DecodedValues(this HttpCookie cookie, string name)
{
if (cookie == null)
{
throw new ArgumentNullException("cookie");
}
return HttpUtility.UrlDecode(cookie.Values[name]);
}
public static void SetEncodedValues(this HttpCookie cookie, string name, string value)
{
if (cookie == null)
{
throw new ArgumentNullException("cookie");
}
cookie.Values[name] = HttpUtility.UrlEncode(value);
}
public static string DecodedValues(this HttpCookie cookie, int index)
{
if (cookie == null)
{
throw new ArgumentNullException("cookie");
}
return HttpUtility.UrlDecode(cookie.Values[index]);
}
}
用法:
if (Request.Cookies["TestCookieValue"] != null)
{
ViewBag.CookieValue = Request.Cookies["TestCookieValue"].DecodedValue();
}
if (Request.Cookies["TestCookieValues"] != null)
{
ViewBag.CookieValues = Request.Cookies["TestCookieValues"].DecodedValues("foo");
ViewBag.CookieValuesIndexed = Request.Cookies["TestCookieValues"].DecodedValues(0);
}
var cookieWithValue = new HttpCookie("TestCookieValue");
cookieWithValue.Expires = DateTime.Now.AddHours(1);
cookieWithValue.SetEncodedValue("Inglês");
Response.SetCookie(cookieWithValue);
var cookieWithValues = new HttpCookie("TestCookieValues");
cookieWithValues.Expires = DateTime.Now.AddHours(1);
cookieWithValues.SetEncodedValues("foo", "Inglês");
Response.SetCookie(cookieWithValues);
请注意HttpUtility.UrlDecode()
是危险的,请使用AntiXSS来阻止Cookie值的跨站点脚本和SQL注入,这可以由客户端任意设置。
您也可能会重新考虑在Cookie中存储Unicode值。您可以轻松地识别该语言,例如通过代码en-US
或其数据库索引(如果适用)。