我想向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 */
我需要做些什么来获得我的自定义模型?
Client
或Client
派生的对象返回给IS4的FindClientByIdAsync()
,就可以将任何所需的内容存储在客户端上。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
。它已经受支持。