public async Task <JwtAuthentificationResponse> Login(LoginRequest requestModel) { var user = await FindUser(requestModel.Email); if (user is null) { throw new ApplicationCustomException(ExceptionConstants.WrongEmailOrPassword); } var result = await _signInManager.PasswordSignInAsync(user, requestModel.Password, requestModel.IsRememberMe, false); if (!result.Succeeded) { throw new ApplicationCustomException(ExceptionConstants.WrongEmailOrPassword); } IEnumerable <string> userRoles = await _userManager.GetRolesAsync(user); JwtAuthentificationResponse response = _jwtHelper.GenerateToken(user, userRoles); return(response); }
public async Task <JwtAuthentificationResponse> RefreshToken(RefreshTokenRequest model) { JwtSecurityToken refreshToken = new JwtSecurityTokenHandler().ReadJwtToken(model.RefreshToken); if (refreshToken.ValidFrom >= DateTime.UtcNow || refreshToken.ValidTo <= DateTime.UtcNow) { throw new UnauthorizeCustomException(ExceptionConstants.UnauthorizeAccess); } string userId = refreshToken.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value; ApplicationUser user = await _userManager.FindByIdAsync(userId); if (user is null) { throw new UnauthorizeCustomException(ExceptionConstants.UnauthorizeAccess); } IEnumerable <string> userRoles = await _userManager.GetRolesAsync(user); JwtAuthentificationResponse response = _jwtHelper.GenerateToken(user, userRoles); return(response); }
public async Task <ActionResult> RefreshToken([FromBody] RefreshTokenRequest request) { JwtAuthentificationResponse response = await _userService.RefreshToken(request); return(Ok(response)); }
public async Task <ActionResult> Login([FromBody] LoginRequest request) { JwtAuthentificationResponse response = await _userService.Login(request); return(Ok(response)); }