首页 > 解决方案 > ApiResource 返回“invalid_scope”身份服务器

问题描述

我在剃刀页面应用程序中实现身份服务器。

请求speechApiResource 时,identityserver 返回“invalid_scope”。我的理解是资源是一组范围。所以,我期待身份服务器返回语音资源中定义的范围。注意:我将其添加speech为 ApiScope 它可以正常工作,但它不会添加speech.synthesizeandpayment.subscription范围。

以下是我定义 ApiScopes 的方式:

public static IEnumerable<ApiScope> ApiScopes =>
        new List<ApiScope>
        {
            new ApiScope("speech.synthesize", "Speech synthesis",new []{"api.create" }),
            new ApiScope("payment.subscription", "Subscription service"),
            new ApiScope("payment.manage", "Manage Payment"),
        };

以下是我定义 ApiResource 的方式:

public static IEnumerable<ApiResource> ApiResources =>
        new List<ApiResource>
        {
            new ApiResource("speech", "Speech API")
            {
                Scopes = { "speech.synthesize", "payment.subscription" }
            }
        };

这是客户端配置:

public static IEnumerable<Client> Clients =>
        new List<Client>
        {
            new Client
            {
                ClientId = "client",

                // no interactive user, use the clientid/secret for authentication
                AllowedGrantTypes = GrantTypes.ResourceOwnerPasswordAndClientCredentials,

                // secret for authentication
                ClientSecrets =
                {
                    new Secret("secret".Sha256())
                },
                AlwaysSendClientClaims = true,
                // scopes that client has access to
                AllowedScopes = {
                    IdentityServerConstants.StandardScopes.OpenId,
                    IdentityServerConstants.StandardScopes.Profile,
                    IdentityServerConstants.StandardScopes.Email,
                    "speech"
                }
            }
        };

这里有什么问题?谁能帮我理解这个问题。

如果不对范围进行分组,Api 资源的作用是什么。

标签: asp.net-coreidentityserver4openid

解决方案


作为客户,您需要 ApiScopes,而不是 ApiResources。另外一个 ApiResource 可以指向一个 ApiScope。

ApiResource 表示 API 实例,而不是 Scope。ApiResources 就像客户端,但对于 APIs。

有关IdentityResource、ApiResource 和 ApiScope 之间区别的更多详细信息,参阅我的答案


推荐阅读