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