Cookies are not working in Google Chrome but working in other browsers like Edge, IE
I am working on Cookies. It was earlier working with all browsers, but now stopped working with Google Chrome and is still working with other browsers like Edge, IE.
Below is my code.
HttpCookie cookie = HttpContext.Current.Request.Cookies["_crt"];
if (cookie == null)
{
cookie = new HttpCookie("_crt");
cookie.Path = "/";
cookie.Expires = DateTime.Now.AddDays(30);
List<Cart> list = cart.AddItemToCart(cart, new List<Cart>());
cookie.Value = cart.EncryptCartItem(list);
HttpContext.Current.Response.Cookies.Add(cookie);
}
else
{
if (string.IsNullOrWhiteSpace(HttpContext.Current.Request.Cookies["_crt"].Value))
{
List<Cart> list = cart.AddItemToCart(cart, new List<Cart>());
cookie.Value = cart.EncryptCartItem(list);
}
else
{ cookie.Value = cart.EncryptCartItem(cart.AddItemToCart(cart, cart.DecryptCartItem(HttpContext.Current.Request.Cookies["_crt"].Value))); }
cookie.Expires = DateTime.Now.AddDays(30);
HttpContext.Current.Response.Cookies.Set(cookie);
Solution 1:
Current versions of Chrome (e.g. v86 *) will reject to save a cookie with SameSite=none
and Secure=false
(or unset), see @Pskyco s reply.
Also, if you serve a site via HTTP (NOT HTTPS) Chrome (and other browsers, tested for Firefox) will not save secure cookies. So, consequently it is not possible to set a SameSite=none
cookie via HTTP in Chrome (as they always have to secure)!
* everything >=v80, according to https://www.chromium.org/updates/same-site
Solution 2:
I have the same issue. Since few days, a simple HttpCookie doesn't work anymore, but only on mobile throught browser.
var cookie = new HttpCookie(CST_IDENTIFICATION_COOKIE)
{
Expires = DateTime.Now.AddDays(3),
};
[...]
Response.SetCookie(cookie);
Btw, I got the answer directly from Chrome console.
A cookie associated with a resource at http://xxxxxx.xxxxxxxx.xxx/ was set with
SameSite=None
but withoutSecure
. A future release of Chrome will only deliver cookies markedSameSite=None
if they are also markedSecure
. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5633521622188032.
I had to set the property 'Secure' of the cookie to 'true' to make it works.
cookie.Secure = true;