2015-02-13 17 views
5

Tôi đang sử dụng cookie để hiển thị một số dữ liệu trong trang tìm kiếm nhưng cookie của tôi đang làm xáo trộn các giá trị khi sử dụng ký tự Unicode. Ví dụ: khi tôi lưu trữ Inglês, tôi nhận được Inglês khi tôi đọc lại.Làm cách nào để lưu trữ an toàn các ký tự Unicode trong cookie?

Đây là cách tôi lưu cookie của tôi:

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); 
} 

Đây là cách tôi lấy nó:

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); 
} 

Khi tôi gỡ lỗi trang web, nó cho thấy giá trị ngay trong mã khi cài đặt nó, nhưng sau khi tôi thực hiện một yêu cầu với jQuery, giá trị đi kèm với các ký tự sai.

Làm cách nào để lưu trữ an toàn các ký tự Unicode trong cookie?

Trả lời

7

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?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ó.

Các vấn đề liên quan