tuitio/IdentityServer.Application/Services/BehaviorService.cs

50 lines
1.8 KiB
C#

using IdentityServer.Application.Services.Abstractions;
using IdentityServer.Application.Stores;
using IdentityServer.Domain.Entities;
using IdentityServer.Domain.Models;
using IdentityServer.Domain.Repositories;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
namespace IdentityServer.Application.Services
{
internal class BehaviorService : IBehaviorService
{
private readonly IServiceProvider _serviceProvider;
private readonly ILogger<BehaviorService> _logger;
private readonly ITokenStore _securityStore;
public BehaviorService(IServiceProvider serviceProvider, ILogger<BehaviorService> logger, ITokenStore securityStore)
{
_serviceProvider = serviceProvider;
_logger = logger;
_securityStore = securityStore;
}
public void FillTokenStore()
=> FillTokenStoreAsync().GetAwaiter().GetResult();
public async Task FillTokenStoreAsync()
{
var activeTokens = Array.Empty<UserToken>();
using (var scope = _serviceProvider.CreateScope())
{
var _repository = scope.ServiceProvider.GetRequiredService<IIdentityRepository>();
activeTokens = await _repository.GetActiveTokens();
}
if (activeTokens.Length <= 0)
return;
_logger.LogInformation($"BehaviorService: {activeTokens.Length} active tokens were found in database.");
foreach (var token in activeTokens)
{
var storeToken = new Token() { Raw = token.Token, ValidFrom = token.ValidFrom, ValidUntil = token.ValidUntil };
_securityStore.SetToken(storeToken, token.UserId);
}
}
}
}