예제 #1
0
        public async Task <ActionResult> RefreshToken([FromBody] RefreshTokenRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            JwtAuthResponse response = await _userService.RefreshToken(request);

            return(Ok(response));
        }
예제 #2
0
        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);
        }
예제 #3
0
        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);
        }