在 ASP.NET 5 中不通过 HTTPS 设置 cookie

Set cookie not over HTTPS in ASP.NET 5

我有两个微服务。一是为了身份。我正在尝试设置身份验证 cookie,并且我有这个中间件:

app.UseCookiePolicy(new CookiePolicyOptions
{
    MinimumSameSitePolicy = SameSiteMode.None,
    Secure = CookieSecurePolicy.None,
    HttpOnly = Microsoft.AspNetCore.CookiePolicy.HttpOnlyPolicy.None
}); 

还有这项服务:

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(options =>
    {
        options.Cookie.HttpOnly = true;
        options.Cookie.SameSite = SameSiteMode.None;
        options.Cookie.SecurePolicy = CookieSecurePolicy.None;
        options.Cookie.IsEssential = true;
    });

并且浏览器也会抛出此警告:

所以我想知道是否可以不通过 HTTPS 设置 cookie ???

您需要通过Https设置cookie,否则将无法使用。

这是因为 Samesite cookie 功能要求在 cookie 到达浏览器时通过 HTTPs 完成。

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite

也就是说:

Cookies with SameSite=None must now also specify the Secure attribute (they require a secure context/HTTPS).