如何将自定义属性添加到 IdentityServer4 PersistedGrantStore

我们将 IPersistedGrantStore 的默认实现与 EntityFramework 和 SQL Server 结合使用。

我需要存储 IP 地址(以获取“登录”上的“大致”位置数据),该表似乎是执行此操作的完美位置,因为它已经存储了客户端 ID、日期时间和刷新令牌的到期时间。是否可以扩展它并添加额外的属性?如果我实现自己的 IPersistedGrantStore 版本,我无法“破坏”接口定义的契约并添加额外的属性,甚至无法使用派生类(来自 IdentityServer4.Models.PersistedGrant),因为这也不会遵守接口。

有什么方法可以向此表添加属性并更新 Grant Store 实现以在调用 StoreAsync 时添加它们?


明月笑刀无情
浏览 66回答 1
1回答

慕哥9229398

只需实现IPersistedGrantStore下面的类似代码,您就可以完全控制持久授权,您可以添加新列来存储。public class PersistStore : IPersistedGrantStore&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; private readonly IPersistedGrandStoreService _persistedGrandStore;&nbsp; &nbsp; &nbsp; &nbsp; public PersistStore(IPersistedGrandStoreService persistedGrandStore)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _persistedGrandStore = persistedGrandStore;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public Task StoreAsync(PersistedGrant grant)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return _persistedGrandStore.AddAsync(grant.ToPersistedGrantModel());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public async Task<PersistedGrant> GetAsync(string key)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var grant = await _persistedGrandStore.GetAsync(key);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return grant.ToPersistedGrant();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public async Task<IEnumerable<PersistedGrant>> GetAllAsync(string subjectId)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var grants = await _persistedGrandStore.GetAllAsync(subjectId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return grants.ToPersistedGrants();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public Task RemoveAsync(string key)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return _persistedGrandStore.RemoveAsync(key);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public Task RemoveAllAsync(string subjectId, string clientId)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return _persistedGrandStore.RemoveAllAsync(subjectId, clientId);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public Task RemoveAllAsync(string subjectId, string clientId, string type)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return _persistedGrandStore.RemoveAllAsync(subjectId, clientId, type);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP