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