The cookie is garbled when the Chinese Logon account is obtained. The cookie is garbled.

Source: Internet
Author: User

The cookie is garbled when the Chinese Logon account is obtained. The cookie is garbled.

Cookie code written after successful login

Response. Cookies ["account"]. Value = account; // "Administrator" Response. Cookies ["account"]. Expires = DateTime. Now. AddDays (14 );

 

Code for reading the cookie before Logon

If (Request. Cookies ["account"]! = Null) ViewData ["account"] = Request. Cookies ["account"]. Value;

In general, if the Cookie value we store is in English, this statement is correct, but if it is in Chinese, it may be garbled during reading. This problem is generally caused by the encoding format problem (that is, the encoding format for saving is inconsistent with the encoding format for reading ), therefore, this problem does not occur if we save and read the encoding format in a unified manner.

The general solution is to unify the UTF-8 encoding format:

Cookie code written after successful login

Response. cookies ["account"]. value = HttpUtility. urlEncode (account, Encoding. getEncoding ("UTF-8"); // "Administrator" Response. cookies ["account"]. expires = DateTime. now. addDays (14 );


Code for reading the cookie before Logon

If (Request. Cookies ["account"]! = Null)
ViewData ["account"] = HttpUtility. UrlDecode (Request. Cookies ["account"]. Value, Encoding. GetEncoding ("UTF-8"); Encoding and decoding must be consistent
System. Web. HttpUtility. UrlDecode and System. Web. HttpUtility. UrlEncode
System. Web. HttpContext. Current. Server. UrlDecode and System. Web. HttpContext. Current. Server. UrlEncode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.