public async Task <ActionResult <AuthenticateDto> > Authenticate([FromBody] AuthenticationModel model, CancellationToken token) { ApiResult <List <Claim> > result = await _mediator.Send(model, token); if (!result.IsSuccess) { return(StatusCode(result.StatusCode)); } var accessToken = CreateAccessToken(CreateJwtClaims(result.Data)); return(new AuthenticateDto() { AccessToken = accessToken, ExpireInSeconds = (int)_configuration.Expiration.TotalSeconds }); }
public async Task <ApiResult <List <Claim> > > Handle(AuthenticationModel request, CancellationToken cancellationToken) { AppUser user = await _userManager.Users.Where(x => x.UserName == request.UserName).FirstOrDefaultAsync(cancellationToken); if (user is null) { return(ApiResult <List <Claim> > .Failed("Username or password is invalid", 400)); } var isValidPassword = await _userManager.CheckPasswordAsync(user, request.Password); if (!isValidPassword) { return(ApiResult <List <Claim> > .Failed("Username or password is invalid", 400)); } IList <Claim> claims = await _userManager.GetClaimsAsync(user); claims.Add(new Claim(ClaimTypes.Name, user.UserName)); claims.Add(new Claim("UserName", user.UserName)); claims.Add(new Claim(ClaimTypes.NameIdentifier, user.Id)); return(ApiResult <List <Claim> > .Success(claims.ToList())); }