我正在尝试将值 AFRSUSERNAME=v@r.com&AFRSPASSWORD=%&v~lyHYNrTCcQq6
存储到 Cookies["AFRSSTATION"]
中。该值已成功保存,我可以使用浏览器查看它。访问值时出现问题。当我尝试获取 returningUser["AFRSUSERNAME"]
的值时,我得到了 v@r.com
和 returningUser["AFRSPASSWORD"]
的值> 是 %
。看起来内部函数根据 &
符号拆分值。我的问题是如何在 Cookie 中保存 & 登录。下面给出相关代码
HttpCookie returningUser = null;
if (HttpContext.Current.Request.Cookies["AFRSSTATION"] != null)
{
returningUser = HttpContext.Current.Request.Cookies["AFRSSTATION"];
if (returningUser["AFRSUSERNAME"] != null &&
returningUser["AFRSUSERNAME"] != "" &&
returningUser["AFRSPASSWORD"] != null &&
returningUser["AFRSPASSWORD"] != "")
{
UserName = returningUser["AFRSUSERNAME"];
Password = returningUser["AFRSPASSWORD"];
最佳答案
不是所有characters are allowed in Cookies , 你可以使用 Server.UrlEncode和 UrlDecode实现这一目标的方法:-
HttpCookie cookie = new HttpCookie("AFRSSTATION");
cookie.Values.Add("AFRSUSERNAME", Server.UrlEncode("v@r.com"));
cookie.Values.Add("AFRSPASSWORD", Server.UrlEncode("%&v~lyHYNrTCcQq6"));
Response.Cookies.Add(cookie);
//Retrieve Cookie values
UserName = Server.UrlDecode(returningUser["AFRSUSERNAME"]);
Password = Server.UrlDecode(returningUser["AFRSPASSWORD"]);
关于c# - 保存 '&' 登录 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30116461/