Chrome SameSite:Identity Server 4(代码流)+Web API Core 3.1+Angel 8 [英] Chrome SameSite: Identity Server 4 (Code flow) + Web API Core 3.1 + Angular 8

查看:14
本文介绍了Chrome SameSite:Identity Server 4(代码流)+Web API Core 3.1+Angel 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的应用程序如下:

  • Identity.Web(本地主机:5555)-.Net Core 3.1:它有用于登录和重置密码的快速页面。我们使用的是Identity Server 4(代码流-OAuth 2.0,OpenID)。
  • Web.Api(本地主机:4500).Net Core 3.1:基本有资源接口
  • 角度8(本地主机:4200):使用Open-id客户端认证和访问资源API。

Chrome SameSite Updates之前,我们的应用程序工作得很好。现在,只要我们输入用户名、密码和登录(标识.Web-本地主机:5555),浏览器就会重定向到(角度8-本地主机:4200),然后直接返回登录页面。

以前有一个auth-回调,然后登录到仪表板。

您可以在下面找到我们的配置:

Identity.Web(config.cs)

        var redirectUris = new List<string> { frontendUrl + "/auth-callback", frontendUrl + "/silent-refresh.html" };

        var allowedCorsOrigins = new List<string> { frontendUrl };

        var postLogoutRedirectUris = new List<string> { frontendUrl + "/signout-callback-oidc" };

        return new List<Client>
        {
           new Client
           {
                RequireConsent = false,
                ClientId = "angular_spa",
                ClientName = "Angular 4 Client",
                AllowedGrantTypes = GrantTypes.Code,
                RequirePkce = true,
                RequireClientSecret = false,
                AllowedScopes = new List<string> {"openid", "profile", "api1"},
                RedirectUris = redirectUris,
                PostLogoutRedirectUris = postLogoutRedirectUris,
                AllowedCorsOrigins = allowedCorsOrigins,
                AllowAccessTokensViaBrowser = true,

           }
        };

Identity.Web(Startup.cs)

 var settings = Configuration.GetSection(nameof(MongoDbSettings)).Get<MongoDbSettings>();

        var mongoDbContext = new MongoDbContext(settings.ConnectionString, settings.DatabaseName);

        services.AddIdentity<ApplicationUser, MongoIdentityRole>()
                .AddMongoDbStores<ApplicationUser, MongoIdentityRole, Guid>(mongoDbContext)
                .AddDefaultTokenProviders();

        services.Configure<MongoSettings>(options =>
        {
            options.ConnectionString = Configuration.GetSection("MongoDbSettings:ConnectionString").Value;
            options.DatabaseName = Configuration.GetSection("MongoDbSettings:DatabaseName").Value;
        });


        services.AddIdentityServer(options => { options.Events.RaiseSuccessEvents = true; })
                .AddDeveloperSigningCredential()
                .AddAspNetIdentity<ApplicationUser>()
                .AddProfileService<ProfileService>()
                .AddMongoRepository()
                .AddClients()
                .AddIdentityApiResources();

Web.Api(Startup.cs)

services.AddAuthentication()
                  .AddIdentityServerAuthentication("api1", options =>
                  {
                      options.Authority = Configuration.GetSection("IdentityServer:BaseUrl").Value;
                      options.RequireHttpsMetadata = false;
                      options.ApiName = "api1";
                      options.TokenRetriever = (request) =>
                      {
                          string token = TokenRetrieval.FromAuthorizationHeader().Invoke(request);
                          if (string.IsNullOrEmpty(token))
                          {
                              token = TokenRetrieval.FromQueryString().Invoke(request);
                          }
                          return token;
                      };
                  });

注意:所有内容在Firefox中都能完美运行。我们阅读了this文章,并应用了其中的内容,但它不起作用。

推荐答案

您将在Google Chrome中看到控制台警告,并且您的身份服务器无法重定向到Chrome版本80的客户端应用。

与资源关联的Cookie设置为SameSite=NONE,但没有SECURE。它已被屏蔽,因为Chrome现在只提供标记为SameSite=None的Cookie,如果它们也被标记为安全的话。您可以在应用程序&>存储&>Cookie下的开发人员工具中查看Cookie,并在https://www.chromestatus.com/feature/5633521622188032查看更多详细信息。

若要解决此问题,您需要执行下面链接中提到的更改以及下面提到的其他更改。

https://www.thinktecture.com/en/identity/samesite/prepare-your-identityserver/

注意:对于.Net Core 2.2,请设置SameSite=(SameSiteMode)(-1);对于.Net Core 3.0或更高版本,请设置SameSite=SameSiteMode.未指定

另外,对于Chrome 80版本,添加此额外条件-

 if ( userAgent.Contains("Chrome/8"))
            {
                return true;
            }

这篇关于Chrome SameSite:Identity Server 4(代码流)+Web API Core 3.1+Angel 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆