首页 > 解决方案 > identityserver4 关联失败

问题描述

最近在学习idenetty server 4, 在github组织了一个测试项目

env: windows 10, edge edge: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 Edg/88.0.705.56

   services.AddAuthentication(options =>
        {
            options.DefaultScheme = "Cookies";
            options.DefaultChallengeScheme = "oidc";
        })
        // 添加可以处理cookie的处理程序
        .AddCookie("Cookies")
        // 用于配置执行OpenID Connect协议的处理程序
        .AddOpenIdConnect("oidc", options =>
        {
            options.Authority = "https://demo.identityserver.io/";    // 受信任令牌服务地址
            options.RequireHttpsMetadata = true;
            options.ClientId = "interactive.confidential";
            options.ClientSecret = "secret";
            options.ResponseType = "code";
            options.SaveTokens = true;  // 用于将来自IdentityServer的令牌保留在cookie中

            // 1、添加授权访问api的支持
            //options.Scope.Add("scope1");
            options.Scope.Add("email");
            //options.Scope.Add("profile");
            options.GetClaimsFromUserInfoEndpoint = true;
            //options.Scope.Add("offline_access");
            options.TokenValidationParameters = new TokenValidationParameters
            {
                NameClaimType = "name",
                RoleClaimType = "role"
            };
        });

问题是我已经登录并授权成功,但是当我跳转回来的时候,会出现如下错误

Exception: Correlation failed.
Unknown location

Exception: An error was encountered while handling the remote login.
Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler<TOptions>.HandleRequestAsync()

在那之后,我看到了一些关于 cookie 的问题。我加了一个ConfigureNonBreakingSameSiteCookies,但还是不行

当我禁用 edge config 时Cookies without SameSite must be secure,它成功了。

问题是Is there any other way?

如果有人能给我一些关于这方面的见解,我将不胜感激。

标签: asp.net-coreidentityserver4openid-connect

解决方案


根据上述评论,这可能是由于您没有使用 HTTPS。该关联 cookie 将设置为SameSite=NoneSameSite=Lax因为它需要在另一个主机发起的请求期间可访问,如果不是由 HTTPS 源发出,Chrome 和 Edge 将默认阻止它。


推荐阅读