如何在本地数据库中存储IdentityServer令牌

时间:2019-04-09 13:54:58

标签: asp.net-mvc identityserver4

我正在使用IdentityServer4为其Web应用程序提供配置身份验证服务。 我已经在memoryStorage中设置了identityServer。 我自问的问题是我是否从identityServer获得了令牌,然后令牌过期后,IdentiyServer重新启动了identitySever是否能够识别该令牌并对其进行续签?

如果是第二个答案,您能给我一些实施方法的想法吗?

2 个答案:

答案 0 :(得分:0)

您需要实现自己的IRefreshTokenStore并将其添加到DI容器中 例如:

// Add to DI
services.AddSingleton<IRefreshTokenStore , CustomRefreshTokenStore >();

// Create a new class that implements the IRefreshTokenStore
public class CustomRefreshTokenStore : IRefreshTokenStore
    {
        public Task<RefreshToken> GetRefreshTokenAsync(string refreshTokenHandle)
        {
            throw new NotImplementedException();
        }

        public Task RemoveRefreshTokenAsync(string refreshTokenHandle)
        {
            throw new NotImplementedException();
        }

        public Task RemoveRefreshTokensAsync(string subjectId, string clientId)
        {
            throw new NotImplementedException();
        }

        public Task<string> StoreRefreshTokenAsync(RefreshToken refreshToken)
        {
            throw new NotImplementedException();
        }

        public Task UpdateRefreshTokenAsync(string handle, RefreshToken refreshToken)
        {
            throw new NotImplementedException();
        }
    }

答案 1 :(得分:0)

IIdentityServerBuilder 接口具有一个扩展名,称为 AddOperationalStore

此处记录了设置和数据库迁移:http://docs.identityserver.io/en/latest/quickstarts/7_entity_framework.html

您可以将其用作:

var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;

var isrvBuilder = services.AddIdentityServer();

isrvBuilder.AddOperationalStore(options =>
{
    options.ConfigureDbContext = builder =>
                builder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"),
                        sql => sql.MigrationsAssembly(migrationsAssembly));
                options.EnableTokenCleanup = true;
                options.TokenCleanupInterval = 30;
});