首页 > 解决方案 > OpenID Connect 和 IdentityServer4:API 与范围

问题描述

http://docs.identityserver.io/en/latest/reference/api_resource.html说“API 必须至少有一个范围。”

IdentityServer4 似乎没有强制执行此限制:我有

public static IEnumerable<ApiResource> GetApis(IConfiguration config)
{
    return new ApiResource[]
    {
        new ApiResource("orm", "Our ORM"),
    };
}

(这里没有范围)并且我的软件确实可以工作并且似乎不会产生任何错误。我是否正确理解这里有错误但 IdentityServer4 只是没有诊断错误?

此外,我有

new Client
{
    ClientId = "mvc",
    ClientName = "MVC Client",

    AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,
    ClientSecrets = { new Secret("XXX".Sha256()) },

    RedirectUris = { ormClientURL + "/signin-oidc" },
    FrontChannelLogoutUri = ormClientURL + "/signout-oidc",
    PostLogoutRedirectUris = { ormClientURL + "/signout-callback-oidc" },

    AllowOfflineAccess = true,
    AllowedScopes = { "openid", "profile", "orm" }
},

尽管我没有"orm"定义范围,但它仍然有效。

为什么它确实有效?

如何正确解决这个问题?(我应该定义哪些客户和范围?)

标签: asp.net-coreidentityserver4openid-connect

解决方案


似乎 ApiResource 构造函数使用提供的名称和 displayName 添加了一个 Scope:

    public ApiResource(string name, string displayName)
        : this(name, displayName, null)
    {
    }

    public ApiResource(string name, string displayName, IEnumerable<string> claimTypes)
    {
        if (name.IsMissing()) throw new ArgumentNullException(nameof(name));

        Name = name;
        DisplayName = displayName;

        Scopes.Add(new Scope(name, displayName));

        if (!claimTypes.IsNullOrEmpty())
        {
            foreach (var type in claimTypes)
            {
                UserClaims.Add(type);
            }
        }
    }

https://github.com/IdentityServer/IdentityServer4/blob/master/src/Storage/src/Models/ApiResource.cs


推荐阅读