如何在IdentityServer4中实现自己的ClientStore



我想在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 */

我需要做什么才能获得我的自定义模型?

我的评论中的建议是可能的解决方案。只要为FindClientByIdAsync()向IS4返回一个有效的ClientClient派生对象,就可以针对客户端存储任何您喜欢的内容。


选项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;它已经得到支持。

最新更新