繁体   English   中英

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

[英]How to add custom properties to the IdentityServer4 PersistedGrantStore

我们在EntityFramework和SQL Server中使用IPersistedGrantStore的默认实现。

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

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

只需像下面的代码那样实现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);
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM