Storing the & & icon in cookies
I am trying to store the value AFRSUSERNAME=v@r.com&AFRSPASSWORD=%&v~lyHYNrTCcQq6
in Cookies["AFRSSTATION"]
. The value is saved successfully and I can see it using the browser. The problem is in accessing values. When I try to get the value returningUser["AFRSUSERNAME"]
I got v@r.com
and the value returningUser["AFRSPASSWORD"]
is %
. It looks like an internal function that splits the value based on the sign &
. My question is how can I save and sign the Cookie. Given below 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"];
source to share
Not all characters are allowed in cookies , you can use Server.UrlEncode and UrlDecode for 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"]);
source to share