tuitio/IdentityServer.Application/CommandHandlers/AuthenticateUserHandler.cs

33 lines
1019 B
C#

using AutoMapper;
using IdentityServer.Application.Commands;
using IdentityServer.Application.Services;
using IdentityServer.PublishedLanguage.Dto;
using MediatR;
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;
public AuthenticateUserHandler(IUserService userService, IMapper mapper)
{
_userService = userService;
_mapper = mapper;
}
public async Task<Token> Handle(AuthenticateUser command, CancellationToken cancellationToken)
{
var internalToken = await _userService.Authenticate(command.UserName, command.Password);
if (internalToken == null)
return null;
var token = _mapper.Map<Token>(internalToken);
return token;
}
}
}