C# Cookie在Google Chrome中不起作用,但在其他浏览器中起作用,如Edge,IE

C# Cookie在Google Chrome中不起作用,但在其他浏览器中起作用,如Edge,IE,c#,cookies,C#,Cookies,我正在做饼干。它以前使用的是所有浏览器,但现在停止使用Google Chrome,仍在使用其他浏览器,如Edge,IE 下面是我的代码 HttpCookie cookie = HttpContext.Current.Request.Cookies["_crt"]; if (cookie == null) { cookie = new HttpCookie("_crt"); cookie.Path = "/"; c

我正在做饼干。它以前使用的是所有浏览器,但现在停止使用Google Chrome,仍在使用其他浏览器,如Edge,IE

下面是我的代码

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);
HttpCookie cookie=HttpContext.Current.Request.Cookies[“\u crt”];
if(cookie==null)
{
cookie=新的HttpCookie(“crt”);
cookie.Path=“/”;
cookie.Expires=DateTime.Now.AddDays(30);
List List=cart.AddItemToCart(cart,newlist());
cookie.Value=cart.EncryptCartItem(列表);
HttpContext.Current.Response.Cookies.Add(cookie);
}
其他的
{
if(string.IsNullOrWhiteSpace(HttpContext.Current.Request.Cookies[“\u crt”].Value))
{
List List=cart.AddItemToCart(cart,newlist());
cookie.Value=cart.EncryptCartItem(列表);
}
其他的
{cookie.Value=cart.EncryptCartItem(cart.AddItemToCart(cart,cart.DecryptCartItem(HttpContext.Current.Request.Cookies[“\u crt”].Value));}
cookie.Expires=DateTime.Now.AddDays(30);
HttpContext.Current.Response.Cookies.Set(cookie);

我也有同样的问题。几天以来,一个简单的HttpCookie不再有效,只能通过浏览器在手机上运行

var cookie = new HttpCookie(CST_IDENTIFICATION_COOKIE)
{
   Expires = DateTime.Now.AddDays(3),
};
[...]
Response.SetCookie(cookie);
顺便说一句,我直接从Chrome控制台得到了答案

与位于的资源关联的cookie 设置为
SameSite=None
但是 没有
Secure
。Chrome的未来版本将只提供cookie 标记的
SameSite=None
如果它们也标记为
Secure
。您可以 查看应用程序>存储>cookies下开发人员工具中的cookies 有关详细信息,请参阅

我必须将cookie的属性“Secure”设置为“true”才能使其正常工作

cookie.Secure=true;

当前版本的Chrome(如v86*)将拒绝使用
SameSite=none
Secure=false
(或取消设置)保存cookie,请参阅@Pskyco的回复

此外,如果您通过HTTP(非HTTPS)为站点提供服务,Chrome(和其他浏览器,针对Firefox进行测试)将不会保存安全cookie。因此,无法通过Chrome中的HTTP设置
SameSite=none
cookie(因为它们始终必须安全)


*一切>=v80,根据

cookies“不工作”是怎么回事?它们没有被接收?没有被存储?如果Chrome的开发者工具控制台看到无效或不推荐使用的cookie,它将显示一条警告消息。Hi-Dai,cookies not received如果它们没有被接收,则表示您的服务器没有发送它们。您确定正在为这些请求执行此代码吗?Hi-Dai,实际上正在运行相同的代码对于IE、Edge、Firefox等其他浏览器,没有理由这样做:浏览器仍然只接受会话cookie(即,cookie没有过期期限,因此当用户关闭网站的所有窗口/选项卡时,它们会被删除)。