保存'&'登录饼干



我试图将值AFRSUSERNAME=v@r.com&AFRSPASSWORD=%&v~lyHYNrTCcQq6存储到Cookies["AFRSSTATION"]中。该值已成功保存,我可以使用浏览器看到它。访问值的问题。当我尝试获取returningUser["AFRSUSERNAME"]的值时,我得到了v@r.comreturningUser["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"];

不是cookie中允许所有字符,您可以使用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"]);

最新更新