同名的Identityserver4 ApiResource和IdentityResource

Identityserver4 ApiResource and IdentityResource with same names

我是 OAuth2 和 OpenID Connect space 的新手,我正在尝试构建 API 并使用 IdentityServer4 保护它。我创建了以下资源:-

public static class MyResources
{
    public static IEnumerable<IdentityResource> GetIdentityResources()
    {
        return new List<IdentityResource>()
        {
            new IdentityResources.OpenId(),
            new IdentityResources.Profile(),
            new IdentityResource("someResource", "some description", new [] { "something" })
        };
    }

    public static IEnumerable<ApiResource> GetApiResources()
    {
        return new List<ApiResource>()
        {
            new ApiResource("someResource", "some other description", new [] { "somethingElse" })
        };
    }
}

如您所见,我有一个名为 someResource 的 IdentityResource,还有一个名为 someResource 的同名 ApiResource。最后,在我的客户端配置中,我定义了以下 客户端范围,其中包括用于访问的一些资源

public static class Clients
{
    public static IEnumerable<Client> GetClients()
    {
        return new[]
        {
            new Client
            {
                ClientId = "MyClientId",
                ClientName = "My Client Name",
                ClientSecrets = new List<Secret> { new Secret("secret".Sha256()) },
                AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,
                RedirectUris = { "http://localhost:5002/signin-oidc" },
                PostLogoutRedirectUris = { "http://localhost:5002" },
                AllowedScopes = new List<string>
                {
                    StandardScopes.OpenId,
                    StandardScopes.Profile,
                    "someResource"
                },
                RequireConsent = true,
                AllowOfflineAccess = true
            }
        };
    }
}

当我运行申请时,这是我在同意屏幕上看到的内容

ID4 Consent Screen

如您在屏幕截图中所见,同意屏幕提供了 IdentityResource 以及名为 someResource 的 ApiResource。这个设计是不是故意的,资源的命名要自己仔细规划,以免发生冲突。我希望 2 个具有相同名称的不同资源至少会引发 运行time 错误,因为客户端范围只能按名称而不是资源类型进行区分。你有什么想法?

范围名称必须是唯一的。

如果您使用 "simplified" 创建资源的方式 - 将隐式创建具有相同名称的范围。

IOW - 不要那样做 ;)