已升级的IdentityServer4-检查预期的作用域openid失败



所有这些都是从我将.Net Core 2.1升级到Core 3.1开始的,为此我还将IdentityServer4 v2.3升级到v4。数据库在迁移和依赖性方面有很多变化。所有工作,除了我现在在尝试使用外部提供程序登录时遇到以下错误。(目前我们只使用外部登录提供商,在这种情况下是谷歌(

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来管理资源和配置。

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

您需要做的是创建ApiScopes来表示用户可以请求访问的作用域。在早期版本中,您的作用域与ApiResources绑定。

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

请参阅此链接:

  • https://github.com/IdentityServer/IdentityServer4/issues/4592

还有一件事,请参阅以下源代码:

其中有两个配置选项,用于控制发现文档中是否包含以下内容:

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

也许ShowIdentityScopes是假的,这就是为什么你看不到它们的原因?

最新更新