BehaviorService fix

master
Tudor Stanciu 2021-11-13 20:58:17 +02:00
parent 19915f05d8
commit a1a19da92a
2 changed files with 18 additions and 5 deletions

View File

@ -14,7 +14,7 @@ namespace IdentityServer.Application
services.AddSingleton<IConfigProvider, ConfigProvider>();
services.AddSingleton<ITokenService, TokenService>();
services.AddScoped<IUserService, UserService>();
services.AddScoped<IBehaviorService, BehaviorService>();
services.AddSingleton<IBehaviorService, BehaviorService>();
}
private static void AddStores(this IServiceCollection services)

View File

@ -1,20 +1,26 @@
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;
private readonly IIdentityRepository _identityRepository;
public BehaviorService(ITokenStore securityStore, IIdentityRepository identityRepository)
public BehaviorService(IServiceProvider serviceProvider, ILogger<BehaviorService> logger, ITokenStore securityStore)
{
_serviceProvider = serviceProvider;
_logger = logger;
_securityStore = securityStore;
_identityRepository = identityRepository;
}
public void FillTokenStore()
@ -22,10 +28,17 @@ namespace IdentityServer.Application.Services
public async Task FillTokenStoreAsync()
{
var activeTokens = await _identityRepository.GetActiveTokens();
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 };