Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving '&' sign in Cookies

Tags:

c#

asp.net

I am trying to store the value [email protected]&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 [email protected] 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"];
like image 972
Vijay Rana Avatar asked Nov 27 '25 16:11

Vijay Rana


1 Answers

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("[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"]);
like image 181
Rahul Singh Avatar answered Nov 29 '25 06:11

Rahul Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!