【问题标题】:Upgraded IdentityServer4 - Checking for expected scope openid failed升级的 IdentityServer4 - 检查预期范围 openid 失败
【发布时间】:2021-01-11 14:35:27
【问题描述】:

一切始于我将 .Net Core 2.1 升级到 Core 3.1,为此我还将 IdentityServer4 v2.3 升级到 v4。 数据库在迁移和依赖方面发生了很多变化。 一切正常,除了我现在尝试使用外部提供程序登录时出现以下错误。 (目前我们只使用外部登录提供程序,在本例中为 Google)

fail: IdentityServer4.Validation.TokenValidator[0]
      Checking for expected scope openid failed
{
        "ValidateLifetime": true,
        "AccessTokenType": "Jwt",
        "ExpectedScope": "openid",
        "Claims": {
          "nbf": 1601023633,
          "exp": 1601023933,
          "iss": "xx-local",
          "client_id": "xx",
          "sub": "89e9001c-dda2-4938-8f3f-4285b160ac42",
          "auth_time": 1601023633,
          "idp": "google",
          "email": "xxxxx@xxxx.com",
          "roles": "tech",
          "iat": 1601023633,
          "scope": [
            "cc",
            "offline_access"
          ],
          "amr": "google"
        }
      }

嗯,错误说它缺少一个必需的范围,即 OpenID,所以我确保在创建客户端时将它添加到我的客户端并在数据库上检查它

new Client
            {
                ClientId = "xx",
                ClientName = " xx Portal",
                AllowedGrantTypes =
                    {
                        GrantType.ResourceOwnerPassword, "external"
                    },
                AllowAccessTokensViaBrowser = true,
                RedirectUris = { },
                PostLogoutRedirectUris = { },
                AllowedScopes = {
                    IdentityServerConstants.StandardScopes.OpenId,
                    IdentityServerConstants.StandardScopes.Profile,
                    "cc"
                },
                RequireConsent = false,
                AllowOfflineAccess = true,
                ClientSecrets = {new Secret("secret".Sha256())},
                RequireClientSecret = false,
                UpdateAccessTokenClaimsOnRefresh = true,
                AccessTokenLifetime = 300,
                RefreshTokenUsage = TokenUsage.ReUse,
                RefreshTokenExpiration = TokenExpiration.Sliding
            },

我还创建了 ApiScopes 和 Resources 来表示范围,它们也在 startup.cs 中注册

    public static IEnumerable<ApiResource> GetApiResources()
    {
        return new List<ApiResource>
        {
            new ApiResource("cc", "CC", new[] { JwtClaimTypes.Subject, JwtClaimTypes.Email }),
        };
    }


    public static IEnumerable<ApiScope> GetApiScopes()
    {
        return new List<ApiScope>
        {
            new ApiScope(name: "cc",   displayName: "CC"),
        };
    }


    public static IEnumerable<IdentityResource> GetIdentityResources()
    {
        return new List<IdentityResource>
        {
            new IdentityResources.OpenId(),
            new IdentityResources.Profile(),
        };
    }

编辑:

我刚刚注意到,在发现文档中,在支持范围列表中,我没有任何 OpenId 或 Profile

  "scopes_supported": [
    "cc",
    "offline_access"
  ],

我正在使用 EF 来管理资源和配置。

我还查看了这里的其他帖子,以及类似的帖子,但不幸的是没有任何帮助!

【问题讨论】:

    标签: asp.net-core identityserver4 openid-connect asp.net-core-3.1


    【解决方案1】:

    您需要做的是创建ApiScopes 来表示用户可以请求访问的范围。在早期版本中,您的范围与 ApiResources 相关联。

    因此,在 v4 中,您有 IdentityResourcesApiScopes 来表示用户可以请求访问的范围。

    查看此链接:

    另一件事,看这个source code

    其中有两个配置选项可控制是否包含在发现文档中:

        /// <summary>
        /// Show identity scopes
        /// </summary>
        public bool ShowIdentityScopes { get; set; } = true;
    
        /// <summary>
        /// Show API scopes
        /// </summary>
        public bool ShowApiScopes { get; set; } = true;
    

    也许 ShowIdentityScopes 是错误的,这就是您看不到它们的原因?

    【讨论】:

    • 谢谢,抱歉,我没有在我的 og 帖子中提供足够的细节,我已经完成了所有这些,但没有任何改变。正如我在帖子的编辑中提到的,我在发现文档的支持范围列表中没有 OpenID 或 Profile
    • 您需要像以前一样将 openid 和 profile 范围添加到您的身份资源以及您的客户端定义中。
    • 当然,我在我的 ApiScopes 中添加了 OpenId 和 Profile 并解决了问题。但是,当检查发现文档时,supported_claims 现在是空的!
    • 这两个范围应该添加到 indentityResource,而不是 AopScopes。否则我猜您会将个人资料信息放入访问令牌中。它应该进入 ID-token 并因此在 ID-token 中被引用
    • 但是我已经有了它们,(请参阅编辑)如果我不将它们放在 ApiScope 中,它们不会出现在发现文档中,结果我得到了同样的错误!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    相关资源
    最近更新 更多