【问题标题】:ApiResource returns "invalid_scope" identityserverApiResource 返回“invalid_scope”身份服务器
【发布时间】:2021-07-15 14:03:21
【问题描述】:

我正在一个剃须刀页面应用程序中实现 Identity Server。

当请求speech ApiResource 时,identityserver 返回“invalid_scope”。我的理解是资源是一组范围。所以,我期待身份服务器返回语音资源中定义的范围。 注意:我将 speech 添加为 ApiScope,它可以正常工作,但不会添加 speech.synthesizepayment.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-core identityserver4 openid


    【解决方案1】:

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

    ApiResource 表示 API 实例,而不是 Scope。 ApiResources 类似于客户端,但用于 API。

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

    【讨论】:

    • 因此,Api Resource 控制可以发送到实际 API 资源的范围列表。阅读您的答案后,我记得 ApiResource 在资源服务器中用作“受众”。
    • 您的客户要求提供一组范围(IdentityResources 或 ApiScopes)。然后,ApiScopes 可以授予对一个或多个 API (ApiResources) 的访问权限。并且访问令牌中的 aud 声明将包含与所选 ApiScope 关联的 ApiRsource 列表。
    • 这有帮助吗?如果您同意,请随时接受我的回答
    • 根据stackoverflow.com/a/63058736/9522887,不再发送受众声明,也不再在资源所有者中配置。这是否意味着不再建议使用 ApiResource?
    • 在第 4 版中,他们引入了 ApiScope 并重新设计了这些东西如何与 ApiResources 一起工作。您确实想要这个,并且您希望观众声称 AddJwtBearer API 使用它来验证他们收到的令牌是为他们准备的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 2018-07-22
    相关资源
    最近更新 更多