简体   繁体   中英

Cookies not read correctly in asp.net core

Browser confirms that both curvlist and guestuser cookies exist. However When I get the guestuser it has the correct value:

string Guest = Request.Cookies["guestuser"]; 

but the cookie curvlist is empty:

在此处输入图片说明 在此处输入图片说明 在此处输入图片说明

The only difference is that guestuser is created in server side, but curvlist created by javascript. But it has nothing to to with reading the cookies

Experimenting different values for the cookie, I found out something that is very stupid (correct me if I'm wrong): Apparently cookie name and value is separated by comma so having commas in the value would lead loss of the rest of the value after comma (in my case starting with comma would lead to empty value). I changed the separator in cookie to $ instead of , and now I'm getting the correct value.

But why Asp.net reads cookies this way? (Note that there was no problem when reading cookies with comma separated values in the javascript .

CookieManager : ASP.Net Core Abstraction layer on top of HTTP Cookie . ASP.NET Core Wrapper to read and write the cookie. it has fluent apis and ease of use.

https://github.com/nemi-chand/CookieManager

Silent features:

  1. it's interface allows you to play with generic object. You don't have to care about casting or serialization.
  2. :The cookie data is protected with the machine key, using security algorithm. For more about data protection.
  3. There are easy options to configure CookieManager. Just add the CookieManager in Configure Service.
  4. Ease to use :The interfaces allows to ease use of read and write http cookie

Have a look

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM