Esempio n. 1
0
        public async Task <RefreshTokenResponseDto> RefreshToken(RefreshTokenInputDto inputDto)
        {
            var signingKey     = AppSettingConfigurationHelper.GetSection("AuthSettings:SecretKey").Value;
            var claimPrincipal = _jwtTokenValidator.GetPrincipalFromToken(inputDto.AccessToken, signingKey);

            if (claimPrincipal != null)
            {
                var id   = claimPrincipal.Claims.First(c => c.Type == "id");
                var user = await _userManager.FindByIdAsync(Convert.ToInt64(id.Value));

                if (_refreshTokenService.HasValidRefreshToken(inputDto.RefreshToken))
                {
                    var jwtToken = await _jwtFactory.GenerateEncodedToken(user.Id.ToString(), user.UserName, user.TenantId);

                    _refreshTokenService.Remove(inputDto.RefreshToken);                   // delete the token we've exchanged
                    var refreshToken = _refreshTokenService.CreateRefreshToken(user, ""); // add the new one
                    return(new RefreshTokenResponseDto(jwtToken, refreshToken.Token, true));
                }
                else
                {
                    return(new RefreshTokenResponseDto(null, "", false, "Invalid Refresh Token"));
                }
            }
            return(new RefreshTokenResponseDto(null, "", false, "Invalid Access Token"));
        }
        public async Task <ActionResult> RefreshToken(RefreshTokenInputDto dto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = await _userManager.FindByEmailAsync(dto.Email);

            if (user == null)
            {
                return(NotFound());
            }

            if (user.RefreshToken != dto.RefreshToken)
            {
                return(BadRequest("Invalid refresh token"));
            }

            var newToken = await _jwtFactory.GenerateEncodedToken(user.Id, user.UserName);

            var newRefreshToken = _tokenFactory.GenerateToken();

            user.RefreshToken = newRefreshToken;
            await _userManager.UpdateAsync(user);

            var response = new LoginOutputDto(newToken, newRefreshToken, true);

            return(Ok(response));
        }
Esempio n. 3
0
        public async Task <UserTokenInfoDto> RefreshAccessToken([FromBody] RefreshTokenInputDto tokenInfo)
        {
            var userValidateDto = await _accountService.GetUserValidateInfo(tokenInfo);

            return(new UserTokenInfoDto
            {
                Token = JwtTokenHelper.CreateAccessToken(_jwtConfig, userValidateDto, tokenInfo.RefreshToken),
                RefreshToken = tokenInfo.RefreshToken
            });
        }
Esempio n. 4
0
        public async Task <IActionResult> RefreshAccessToken([FromBody] RefreshTokenInputDto tokenInfo)
        {
            var userValidateDto = await _accountService.GetUserValidateInfo(tokenInfo);

            return(new OkObjectResult(new
            {
                Token = JwtTokenHelper.CreateAccessToken(_jwtConfig, userValidateDto, tokenInfo.RefreshToken),
                tokenInfo.RefreshToken
            }));
        }
Esempio n. 5
0
        public async Task <UserValidateDto> GetUserValidateInfo(RefreshTokenInputDto tokenInfo)
        {
            var user = await _userRepository.FetchAsync(x => new { x.Name, x.Email, x.RoleId, x.Account, x.ID, x.Status }, x => x.Account == tokenInfo.Account);

            if (user == null)
            {
                throw new BusinessException(new ErrorModel(ErrorCode.NotFound, "用户不存在,参数信息不完整"));
            }

            return(_mapper.Map <UserValidateDto>(user));
        }
Esempio n. 6
0
        public async Task <ActionResult> RefreshToken(RefreshTokenInputDto inputDto)
        {
            var response = await _authService.RefreshToken(inputDto);

            return(new ContentResult
            {
                ContentType = "application/json",
                Content = JsonSerializer.Serialize(response),
                StatusCode = (int)(response.Success ? HttpStatusCode.OK : HttpStatusCode.InternalServerError)
            });
        }
Esempio n. 7
0
        public async Task <AppSrvResult <UserValidateDto> > GetUserValidateInfo(RefreshTokenInputDto tokenInfo)
        {
            var user = await _userRepository.FetchAsync(x => new { x.Name, x.Email, x.RoleId, x.Account, x.ID, x.Status }, x => x.Account == tokenInfo.Account);

            if (user == null)
            {
                return(Problem(HttpStatusCode.NotFound, "用户不存在,参数信息不完整"));
            }

            return(_mapper.Map <UserValidateDto>(user));
        }
        public ActionResult RefreshToken(RefreshTokenInputDto refreshTokenInput)
        {
            TokenOutputDto token = null;

            if (IsModelValid() && refreshTokenInput.GrantType.ToLower().Equals("refresh_token"))
            {
                var user = _service.FindById(CurrentUser);
                token = _tokenService.RenewToken(refreshTokenInput, user);
            }

            return(CustomizeResponse(token));
        }
Esempio n. 9
0
        public async Task <ActionResult <UserTokenInfoDto> > RefreshAccessToken([FromBody] RefreshTokenInputDto tokenInfo)
        {
            var result = await _accountService.GetUserValidateInfo(tokenInfo);

            if (result.IsSuccess)
            {
                return(Ok(new UserTokenInfoDto
                {
                    Token = JwtTokenHelper.CreateAccessToken(_jwtConfig, result.Content, tokenInfo.RefreshToken),
                    RefreshToken = tokenInfo.RefreshToken
                }));
            }

            return(Problem(result.ProblemDetails));
        }
Esempio n. 10
0
        public TokenOutputDto RenewToken(RefreshTokenInputDto refreshTokenInput, User user)
        {
            string refreshTokenStr = _cache.GetString(refreshTokenInput.RefreshToken);

            if (!string.IsNullOrWhiteSpace(refreshTokenStr))
            {
                var refreshTokenData = JsonConvert.DeserializeObject <RefreshTokenDto>(refreshTokenStr);

                if (refreshTokenData != null)
                {
                    _cache.Remove(refreshTokenInput.RefreshToken);
                    return(Generation(user));
                }
            }

            return(null);
        }
        public async Task <IActionResult> RefreshJWToken([FromBody] RefreshTokenInputDto request)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(request.timeZone))
                {
                    request.timeZone = request.timeZone.Trim();
                }

                var jwtData = await _accountService.RefreshJWTokenAsync(request.token, request.refreshToken, request.timeZone);

                var user = await _userManager.FindByIdAsync(jwtData.userId);

                if (user == null)
                {
                    return(BadRequest(new ApiResponse(400, "User of this token is not existing.")));
                }

                if (!string.IsNullOrEmpty(request.timeZone) && user != null)
                {
                    await _userService.UpdateTimezoneCurrentUser(user.Id, request.timeZone);
                }
                //Update User Last Login Date
                if (request.lastLocalTimeLoggedIn != null)
                {
                    if (request.lastLocalTimeLoggedIn.HasValue)
                    {
                        await _userService.UpdateUserLastLoginDate(user.Id, request.lastLocalTimeLoggedIn.Value);
                    }
                }

                if (!string.IsNullOrEmpty(jwtData.Item1))
                {
                    return(Ok(new { token = jwtData.NewToken, refreshToken = jwtData.NewRefreshToken, timeZone = jwtData.TimeZone }));
                }
                else
                {
                    return(BadRequest(new ApiResponse(400, "Invalid token or refresh token.")));
                }
            }
            catch
            {
                return(BadRequest(new ApiResponse(400, "Invalid token or refresh token.")));
            }
        }
Esempio n. 12
0
        public TokenOutputDto RenewToken(RefreshTokenInputDto refreshTokenInput, User user)
        {
            string refreshTokenStr = _cache.GetString(refreshTokenInput.RefreshToken);

            if (string.IsNullOrWhiteSpace(refreshTokenStr))
            {
                AddError("Não foi possível renovar o seu token.");
            }
            else
            {
                var refreshTokenData = JsonConvert.DeserializeObject <RefreshTokenDto>(refreshTokenStr);

                if (refreshTokenData != null)
                {
                    RemoveCache(refreshTokenInput.RefreshToken);
                    return(Generation(user));
                }
            }

            return(null);
        }