【问题标题】:IdentityServer4 ResourceStore - IdentityResource vs ApiResourceIdentityServer4 ResourceStore - IdentityResource vs ApiResource
【发布时间】:2017-05-24 19:40:13
【问题描述】:

我正在尝试设置 IdentityServer4 以使用我自己的 (mongodb) 数据库,而不是文档中显示的内存示例。

为此,我配置了以下服务:

builder.Services.AddTransient<IPersistedGrantStore, PersistedGrantStore>();
builder.Services.AddTransient<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();
builder.Services.AddTransient<IClientStore, ClientStore>();
builder.Services.AddTransient<IResourceStore, ResourceStore>();

在我的数据库中,我创建了 4 个集合:“ApiResources”、“IdentityResources”和“Clients”。

在 ApiResources 中,我定义了我要保护的 API:

{ 
    "Name" : "MyAPI", 
    "DisplayName" : "Test API Resource"
}

在 IdentityResources 中,我定义了我的身份:

{ 
    "Name" : "MyIdentity", 
    "DisplayName" : "Test Identity Resource"
}

我已经定义了以下客户端:

{ 
    "ClientId" : "client", 
    "Enabled" : true, 
    "ClientSecrets" : [
        {
            "Description" : null, 
            "Value" : "K7gNU3sdo+OL0wNhqoVWhr3g6s1xYv72ol/pe/Unols=", 
            "Expiration" : null, 
            "Type" : "SharedSecret"
        }
    ], 
    "ClientName" : null, 
    "ClientUri" : null, 
    "LogoUri" : null, 
    "RequireConsent" : true, 
    "AllowRememberConsent" : true, 
    "AllowedGrantTypes" : [
        "client_credentials"
    ], 
    "AllowedScopes" : [
        "MyAPI"
    ], 
    "Claims" : [

    ], 
    "AllowedCorsOrigins" : [

    ]
}

我的数据库表示类似于文档中的示例。

在我的IResourceStore 实现中,对于FindIdentityResourcesByScopeAsync,我在我的IdentityResources 集合中查找范围名称(正如方法名称所暗示的那样),在我的FindApiResourcesByScopeAsync 中我在我的@987654329 中查找范围名称@collection 顾名思义。

当我尝试针对服务器验证客户端时,我收到了Requested scope not allowed: MyAPI

但如果我将FindIdentityResourcesByScopeAsync 中的代码更改为ApiResources,那么它就可以工作了。

这是一个错误吗?还是我不明白 IdentityResources 和 ApiResources 之间有什么区别?什么时候应该使用每个?如果在FindIdentityResourcesByScopeAsync 中我应该获取我的API 资源,我应该在FindApiResourcesByScopeAsync 中获取什么?

【问题讨论】:

  • 我希望 Base64 密码是假的/正在测试的。
  • @Falk 不用担心 - 确实如此。这是默认的教程哈希。

标签: identityserver4


【解决方案1】:

所以我终于弄清楚了问题所在。虽然在被问到 FindIdentityResourcesByScopeAsync 时返回 API 资源 - 这显然不是要走的路。

我终于注意到问题实际上出在FindApiResourcesByScopeAsync返回的ApiResource对象中。当它返回一个带有我要授予访问权限的 API 名称的 ApiResource 时,该对象不包含 Scopes 的任何值,它还应该包含 MyAPI 的记录。

这里我不明白的是这个Scopes 对象是什么。以及为什么它应该再次包含MyAPI 定义(如父对象)。我应该/可以在这里添加什么其他Scopes,它们的含义是什么?

【讨论】:

  • Scope 用于客户端不应访问整个 API(“资源”)但只能访问其中一部分的情况。 ApiResource 用于授予对整个 API 的访问权限。
  • 即。可能有new ApiResource("MyApi", "My API"),以及new Scope("MyApi.ReadWrite", "Read-write access to my API")new Scope("MyApi.ReadOnly", "Read-only access to my API")
猜你喜欢
  • 2020-12-27
  • 2019-01-07
  • 1970-01-01
  • 2020-12-07
  • 1970-01-01
  • 2018-02-02
  • 2021-08-08
  • 2019-07-10
  • 1970-01-01
相关资源
最近更新 更多