tuitio/IdentityServer.Application/CommandHandlers/AuthenticateUserHandler.cs

40 lines
1.4 KiB
C#
Raw Normal View History

2020-12-20 03:06:43 +02:00
using AutoMapper;
using IdentityServer.Application.Commands;
using IdentityServer.Application.Services;
using IdentityServer.PublishedLanguage.Dto;
using MediatR;
2020-12-21 01:28:22 +02:00
using Microsoft.Extensions.Logging;
2020-12-20 03:06:43 +02:00
using System.Threading;
using System.Threading.Tasks;
namespace IdentityServer.Application.CommandHandlers
{
public class AuthenticateUserHandler : IRequestHandler<AuthenticateUser, Token>
{
private readonly IUserService _userService;
private readonly IMapper _mapper;
2020-12-21 01:28:22 +02:00
private readonly ILogger<AuthenticateUserHandler> _logger;
2020-12-20 03:06:43 +02:00
2020-12-21 01:28:22 +02:00
public AuthenticateUserHandler(IUserService userService, IMapper mapper, ILogger<AuthenticateUserHandler> logger)
2020-12-20 03:06:43 +02:00
{
_userService = userService;
_mapper = mapper;
2020-12-21 01:28:22 +02:00
_logger = logger;
2020-12-20 03:06:43 +02:00
}
public async Task<Token> Handle(AuthenticateUser command, CancellationToken cancellationToken)
{
var internalToken = await _userService.Authenticate(command.UserName, command.Password);
if (internalToken == null)
2020-12-21 01:28:22 +02:00
{
_logger.LogDebug($"Authentication failed for user '{command.UserName}'.");
2020-12-20 03:06:43 +02:00
return null;
2020-12-21 01:28:22 +02:00
}
2020-12-20 03:06:43 +02:00
2020-12-21 01:28:22 +02:00
_logger.LogDebug($"Authentication succeeded for user '{command.UserName}'.");
2020-12-20 03:06:43 +02:00
var token = _mapper.Map<Token>(internalToken);
return token;
}
}
}