如何在IdentityServer4中实现自己的ClientStore?

问题描述 投票:0回答:1

我想向IdentityServer4中的Client实体添加一些额外的列(例如ClientCustomProperty),并在我的业务层中对其进行处理,所以我要像这样创建自定义商店:

public class MyClientStore : IClientStore
{
    public Task<IdentityServer4.Models.Client> FindClientByIdAsync(string clientId)
    {
         // ...
    }
}

我想用商店中的额外列(不是IdentityServer4.Models.Client)返回我的拥有模型,但是IClientStore.FindClientByIdeAsync签名是:

Task<IdentityServer4.Models.Client> FindClientByIdAsync(string clientId);

我认为应该是这样的(通用):

Task<TModel> FindClientByIdAsync<TModel>(string clientId)
    where TModel: class, IClientModel /* IClientModel is in IS4 */

我需要做些什么来获得我的自定义模型?

c# entity-framework-core identityserver4
1个回答
0
投票
我评论中的建议是可能的解决方案。只要您将有效的ClientClient派生的对象返回给IS4的FindClientByIdAsync(),就可以将任何所需的内容存储在客户端上。


选项1:从Client派生:

public MyClient : Client { public string MyExtraProperty { get; set; } } Task<Client> FindClientByIdAsync(string clientId) { MyClient result = // fetch your client here; return result; }


选项2:适应Client

public MyClient { // Properties that Client requires, or can be adapted to what Client requires, here. // ... public string MyExtraProperty { get; set; } } Task<Client> FindClientByIdAsync(string clientId) { MyClient result = // fetch your client here; return Adapt(result); } private Client Adapt(MyClient value) { return // your-client-adapted-to-Client here; }

由于Client已经包含很多数据,所以此选项没有其他意义。


选项3:添加到Properties

这里您将其他数据添加到Client.Properties集合中。 IS4将忽略它,但是您可以在Client实例可用的任何地方访问数据。此选项不需要自定义类型,甚至不需要自定义IClientStore。它已经受支持。

© www.soinside.com 2019 - 2024. All rights reserved.