コード例 #1
0
        public async Task <IActionResult> RefreshTokens([FromBody] TokensModel item)
        {
            if (!ModelState.IsValid)
            {
                return(StatusCode(422, "Помилкові данні."));
            }
            if (string.IsNullOrEmpty(item.AccessToken))
            {
                return(StatusCode(422, "Відсутній токен доступу."));
            }
            if (string.IsNullOrEmpty(item.RefreshToken))
            {
                return(StatusCode(422, "Відсутній токен оновлення."));
            }
            if (item.ExpiresIn == 0)
            {
                return(StatusCode(422, "Час дії токену завершився."));
            }
            var result = await _accountRepository.RefreshToken(item);

            if (result.Result == OperationResult.Ok)
            {
                return(StatusCode(200, result.Value));
            }

            return(StatusCode(422, result.Message));
        }
コード例 #2
0
        public async Task UpdateTokensAsync(ITurnContext turnContext, TokensModel newTokens)
        {
            newTokens.ExpiresAt       = DateTime.Now.AddSeconds(newTokens.ExpiresIn);
            newTokens.UserId          = new Guid(newTokens.IdToken.GetClaim(TokenClaimConstants.UserIdClaim));
            newTokens.UserName        = newTokens.IdToken.GetClaim(TokenClaimConstants.UserNameClaim);
            newTokens.SchoolId        = new Guid(newTokens.IdToken.GetClaim(TokenClaimConstants.SchoolIdClaim));
            newTokens.IsAuthenticated = true;

            await this.UserTokensAccessor.SetAsync(turnContext, newTokens);
        }
コード例 #3
0
        public async Task <IActionResult> Refresh(TokensModel model)
        {
            var(token, refreshToken, isExpired) = await _accountService.Refresh(model);

            if (isExpired)
            {
                return(Unauthorized());
            }

            if (string.IsNullOrEmpty(token))
            {
                return(BadRequest());
            }

            return(new ObjectResult(new
            {
                token,
                refreshToken
            }));
        }
コード例 #4
0
        public async Task <(string token, string refershToken, bool isExpired)> Refresh(TokensModel model)
        {
            var principal = _tokenService.GetPrincipalFromExpiredToken(model.Token);
            var username  = principal.Identity.Name; //this is mapped to the Name claim by default

            var user = await _accountDataService.GetUserByUserName(username);

            if (user == null || user.RefreshToken != model.RefreshToken)
            {
                return(null, null, true);
            }

            var IsExpired = user.RefreshTokenExpDate < DateTime.Now;

            if (IsExpired)
            {
                return(null, null, true);
            }

            var newJwtToken     = _tokenService.GenerateAccessToken(principal.Claims);
            var newRefreshToken = _tokenService.GenerateRefreshToken();

            var refreshTokenExpDate = DateTime.Now.AddMinutes(
                int.Parse(_configuration["Jwt:RefreshTokenDurationInMinutes"]));

            await _accountDataService.UpdateUserRefreshToken(user, newRefreshToken, refreshTokenExpDate);

            return(newJwtToken, newRefreshToken, false);
        }
コード例 #5
0
 private void InsertTokens(TokensModel tokens)
 {
     Response.Cookies.Append(JwtConstants.AccessToken, tokens.AccessToken);
     Response.Cookies.Append(JwtConstants.RefreshToken, tokens.RefreshToken);
 }