2015-05-08 106 views
3

我試圖將[email protected]&AFRSPASSWORD=%&v~lyHYNrTCcQq6的值存儲到Cookies["AFRSSTATION"]。值已成功保存,我可以使用瀏覽器查看它。訪問值時遇到問題。當我嘗試獲得returningUser["AFRSUSERNAME"]的值時,我得到了[email protected],值returningUser["AFRSPASSWORD"]%。它看起來像內部函數在&符號的基礎上分割值。我的問題是我如何保存在Cookie中登錄&。由於波紋管是相關的代碼保存'&'登錄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

的可能重複://stackoverflow.com/questions/4125807/broken-string-in-cookie-after-ampersand-javascript) – David

回答

1

並非所有characters are allowed in Cookies,您可以使用Server.UrlEncodeUrlDecode方法來實現這一目標: - (HTTP [符號(JavaScript)的破碎後的字符串中的cookie]

HttpCookie cookie = new HttpCookie("AFRSSTATION"); 
cookie.Values.Add("AFRSUSERNAME", Server.UrlEncode("[email protected]")); 
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"]);