Xem 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? và Allowed characters in cookies để giải thích lý do tại sao bạn cần phải mã hóa giá trị cookie.
Tóm lại: Ký tự Unicode trong tiêu đề (trong đó cookie được gửi) được hỗ trợ bởi hầu hết các trình duyệt, nhưng không phải tất cả. Một số trình duyệt giải thích các byte Unicode dưới dạng ASCII, dẫn đến Mojibake.
jQuery cũng có vẻ đóng một vai trò theo một số câu hỏi được liên kết, nhưng tôi không thể tái tạo điều đó.
Vì vậy, để lưu trữ an toàn các ký tự Unicode (hoặc bất kỳ ký tự không phải ASCII hoặc ký tự điều khiển nào) trên tất cả các trình duyệt, bạn cần phải mã hóa các ký tự. Điều này có thể được thực hiện thông qua, ví dụ, base64 và phần trăm mã hóa.
An thực hiện sau này, hơi chuyển thể từ 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]);
}
}
Cách sử dụng:
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);
Xin lưu ý HttpUtility.UrlDecode()
là nguy hiểm, sử dụng AntiXSS để ngăn chặn Cross-site Scripting và SQL injection từ giá trị cookie, có thể được thiết lập tùy ý bởi máy khách.
Bạn cũng có thể xem xét lại việc lưu trữ các giá trị Unicode trong cookie. Bạn có thể dễ dàng xác định ngôn ngữ khác, ví dụ: thông qua mã số en-US
hoặc chỉ mục cơ sở dữ liệu của nó nếu có.