Cookie“ .AspNetCore.Identity.Application”已设置为“ SameSite = None”,还必须设置为“ Secure”。

12
我按照以下链接所述进行了操作:

这是我的设置:

services.AddIdentityServer()
    .AddApiAuthorization<ApplicationUser, ApplicationDbContext>();

services.AddAuthentication()
    .AddIdentityServerJwt();

services.ConfigureNonBreakingSameSiteCookies();

// Adjust to this (or similar)
services
    .AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(options =>
     {
        // add an instance of the patched manager to the options:
        options.CookieManager = new ChunkingCookieManager();
      });

然后在配置文件中:

app.UseCookiePolicy();

我正在尝试在HTTP上运行身份验证。当设置特定(但不是所有)cookie时,会出现这些错误,并且我在Chrome中无法完全删除cookie。

2个回答

11

如果其他人遇到同样的问题,我用类似的方法修改了NonceCookie和CorrelationCookie属性以使它们工作。我们的系统使用Identity Server,位于Load Balancer后面,该负载均衡器还卸载SSL。

services.AddAuthentication(options =>
{
   options.DefaultScheme = "cookies";
   options.DefaultChallengeScheme = "oidc";
})
.AddCookie("cookies", options =>
{
   options.Cookie.Name = "appcookie";
   options.Cookie.SameSite = SameSiteMode.Strict;
   options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
})
.AddOpenIdConnect("oidc", options =>
{
   options.NonceCookie.SecurePolicy = CookieSecurePolicy.Always;
   options.CorrelationCookie.SecurePolicy = CookieSecurePolicy.Always;
...
}

SameSiteMode.Strict 对你有用吗?难道不应该是 None,这样身份验证服务器才能在客户端上设置它吗? - Guy Levy
2
@GuyLevy 奇怪的是,将SameSiteMode设置为Strict后它能够正常工作。我本来以为它会因为这个设置而失败,但实际上并没有。 - cminus
今天的情况下,对于.NET 7和最新的Identity Server包,在Chrome中对我不起作用。但是Firefox没有这个问题。 Microsoft.AspNetCore.Authentication.OpenIdConnect 7.0.0Duende.IdentityServer 6.1.7 - diegosasw

7

你的代码没有问题,但你需要更多地配置你的cookies。

AddCookie中添加额外的属性 - SecureHttpOnlySameSite。 更多信息请参见官方文档

示例:

        services
           .AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
           .AddCookie(options =>
           {
               // add an instance of the patched manager to the options:
               options.CookieManager = new ChunkingCookieManager();

                options.Cookie.HttpOnly = true;
                options.Cookie.SameSite = SameSiteMode.None;
                options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
           });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接