3

I am trying to store the value AFRSUSERNAME=v@r.com&AFRSPASSWORD=%&v~lyHYNrTCcQq6 into Cookies["AFRSSTATION"]. The value is saved successfully and i can see it using browser. Problem in accessing the values. When i try to get the value of returningUser["AFRSUSERNAME"] i got v@r.com and value of returningUser["AFRSPASSWORD"] is %. It looks like internal function spliting the value on the bases of & sign. My question is how can i save the & sign in Cookie. Given bellow is related code

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"];
Vijay Rana
  • 123
  • 1
  • 3
  • 13
  • 1
    possible duplicate of [Broken string in cookie after ampersand (javascript)](http://stackoverflow.com/questions/4125807/broken-string-in-cookie-after-ampersand-javascript) – David May 08 '15 at 06:08

1 Answers1

1

Not all characters are allowed in Cookies, you can use Server.UrlEncode and UrlDecode methods to achieve this:-

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"]);
Community
  • 1
  • 1
Rahul Singh
  • 21,585
  • 6
  • 41
  • 56