IdentityServer4资源存储-IdentityResource与ApiResource

IdentityServer4资源存储-IdentityResource与ApiResource,identityserver4,Identityserver4,我试图将IdentityServer4设置为使用我自己的(mongodb)数据库,而不是文档中显示的内存中的示例 为此,我配置了以下服务: builder.Services.AddTransient<IPersistedGrantStore, PersistedGrantStore>(); builder.Services.AddTransient<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator&g

我试图将IdentityServer4设置为使用我自己的(mongodb)数据库,而不是文档中显示的内存中的示例

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

builder.Services.AddTransient<IPersistedGrantStore, PersistedGrantStore>();
builder.Services.AddTransient<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();
builder.Services.AddTransient<IClientStore, ClientStore>();
builder.Services.AddTransient<IResourceStore, ResourceStore>();
在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
中,我在我的
ApiResources
集合中查找作用域,顾名思义

当我尝试针对服务器对客户端进行身份验证时,我得到了
请求的范围不允许:MyAPI

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


这是虫子吗?或者我不知道IdentityResources和ApiResources之间到底有什么区别吗?什么时候使用?如果在
FindIdentityResourcesByScopeAsync
中我应该获取API资源,那么在
FindApiResourcesByScopeAsync
中我应该获取什么呢?

因此我最终找到了问题所在。尽管在被询问时返回API资源
FindIdentityResourcesByScopeAsync
,但这显然不是正确的方法

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


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

因此,我终于找到了问题所在。尽管在被询问时返回API资源
FindIdentityResourcesByScopeAsync
,但这显然不是正确的方法

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


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

当客户端不应该访问整个API(“资源”),而只能访问其中的一部分时,使用
范围。
apirource
用于授予对整个API.ie的访问权限。可能存在
新的API资源(“MyApi”、“My API”)
,以及
新作用域(“MyApi.ReadWrite”、“对My API的读写访问”)
新作用域(“MyApi.ReadOnly”、“对My API的只读访问”)
。当客户端不应该访问整个API(“资源”)时,使用
作用域,但这只是其中的一部分。
apirource
用于授予对整个API.ie的访问权限。可能有一个
新的ApiResource(“MyApi”、“MyApi”)
,以及
新的作用域(“MyApi.ReadWrite”、“对我的API的读写访问”)
新的作用域(“MyApi.ReadOnly”、“对我的API的只读访问”)
。我希望Base64秘密是假的/正在测试的。@Falk不必担心-确实如此。这是默认的教程散列。我希望Base64秘密是假的/正在测试的。@Falk别担心-是的。这是默认的教程哈希。
{ 
    "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" : [

    ]
}