拯救'&'签到饼干

时间:2015-05-08 05:32:13

标签: c# asp.net

我正在尝试将值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"];

1 个答案:

答案 0 :(得分:1)

并非所有characters are allowed in Cookies,您都可以使用Server.UrlEncodeUrlDecode方法来实现此目标: -

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"]);