【问题标题】:How to add custom properties to the IdentityServer4 PersistedGrantStore如何将自定义属性添加到 IdentityServer4 PersistedGrantStore
【发布时间】:2023-03-15 20:11:01
【问题描述】:
我们将 IPersistedGrantStore 的默认实现与 EntityFramework 和 SQL Server 一起使用。
我需要存储 IP 地址(以在“登录”上获取“球场”位置数据)此表似乎是这样做的理想场所,因为它已经存储了客户端 ID、日期时间和到期时间刷新令牌。是否可以扩展它并添加额外的属性?如果我实现自己的 IPersistedGrantStore 版本,我不能“破坏”接口定义的合同并添加额外的属性,甚至不能使用派生类(来自 IdentityServer4.Models.PersistedGrant),因为它也不会遵守接口.
有什么方法可以在此表中添加属性并更新 Grant Store 实现以在调用 StoreAsync 时添加它们?
【问题讨论】:
标签:
c#
entity-framework
.net-core
identityserver4
【解决方案1】:
只需像下面的代码一样实现您的IPersistedGrantStore,您就可以完全控制持久授权,您可以添加新列来存储。
public class PersistStore : IPersistedGrantStore
{
private readonly IPersistedGrandStoreService _persistedGrandStore;
public PersistStore(IPersistedGrandStoreService persistedGrandStore)
{
_persistedGrandStore = persistedGrandStore;
}
public Task StoreAsync(PersistedGrant grant)
{
return _persistedGrandStore.AddAsync(grant.ToPersistedGrantModel());
}
public async Task<PersistedGrant> GetAsync(string key)
{
var grant = await _persistedGrandStore.GetAsync(key);
return grant.ToPersistedGrant();
}
public async Task<IEnumerable<PersistedGrant>> GetAllAsync(string subjectId)
{
var grants = await _persistedGrandStore.GetAllAsync(subjectId);
return grants.ToPersistedGrants();
}
public Task RemoveAsync(string key)
{
return _persistedGrandStore.RemoveAsync(key);
}
public Task RemoveAllAsync(string subjectId, string clientId)
{
return _persistedGrandStore.RemoveAllAsync(subjectId, clientId);
}
public Task RemoveAllAsync(string subjectId, string clientId, string type)
{
return _persistedGrandStore.RemoveAllAsync(subjectId, clientId, type);
}
}