0

I want to set cookie using asp.net core. i use this code to add cookie

CookieOptions options = new CookieOptions();
options.Expires = DateTime.Now.AddDays(5);
Response.Cookies.Append("email", "hello@gmail.com", options);

But in cookie email is not store as it is . its like hello%gmail.com . I can store the url and email into cookie.

Any help. Thank you

Hasib
  • 141
  • 9
  • I believe it helps! - https://stackoverflow.com/questions/4125807/broken-string-in-cookie-after-ampersand-javascript – E. Biagi May 05 '21 at 14:31

1 Answers1

0

As far as I know, if you store the cookie in the response, it will use url encrypt the cookie by default.

When you checked the client cookie, it will store as below:

enter image description here

This will not affect reading cookie in your asp.net core backend codes.

The source codes will decrypt the cookie value by default to let you see the result.

More details, you could refer to below example:

        var re = Request.Cookies["email"];

Result:

enter image description here

I try to read the cookie using Javascript and it seems it not show as it should

If you want to read the cookie from js, you should use decodeURIComponent method to decode it.

More details, you could refer to below codes:

    $(document).ready(function () {

        var read_cookies = document.cookie;
        var split_read_cookie = read_cookies.split(";");
        for (i = 0; i < split_read_cookie.length; i++) {
            var value = split_read_cookie[i];
            value = value.split("=");
            if (value[0] == " email" ) {
                alert(value[1]);
                alert(decodeURIComponent(value[1]));
            }
        }
                  });

Result:

enter image description here

Brando Zhang
  • 22,586
  • 6
  • 37
  • 65