private async Task <IActionResult> RefreshToken(TokenRequestViewModel viewModel)
        {
            var refreshToken =
                await _tokenRepository.CheckRefreshTokenForClient(viewModel.ClientId, viewModel.RefreshToken);

            if (refreshToken == null)
            {
                return(new UnauthorizedResult());
            }

            var user = await _userAndRoleRepository.GetUserById(refreshToken.UserId);

            if (user == null)
            {
                return(new UnauthorizedResult());
            }

            var newRefreshToken = _tokenService.GenerateRefreshToken(refreshToken.ClientId, refreshToken.UserId);
            await _tokenRepository.RemoveRefreshToken(refreshToken);

            await _tokenRepository.AddRefreshToken(newRefreshToken);

            var tokenData = _tokenService.CreateAccessToken(newRefreshToken.UserId);

            var response = new TokenResponseViewModel
            {
                Expiration   = tokenData.ExporationTimeInMinutes,
                RefreshToken = newRefreshToken.Value,
                Token        = tokenData.EncodedToken
            };

            return(Json(response));
        }
Ejemplo n.º 2
0
        public async Task <RefreshToken> GenerateRefreshToken(User user)
        {
            var token = new RefreshToken
            {
                Token     = GenerateRefreshToken(),
                ExpiresAt = DateTime.UtcNow.AddDays(7),
                User      = user
            };

            await _tokenRepository.AddRefreshToken(token);

            return(token);
        }