Exemplo n.º 1
0
        public async Task <ApiResult <JsonWebToken> > RefreshToken([FromQuery] BasicInput basicInput, [FromQuery] string refreshToken)
        {
            if (refreshToken.IsNullOrEmpty())
            {
                return(Result <JsonWebToken>(DefaultStatusCode.ParametersError));
            }

            ClaimsPrincipal principal;

            try
            {
                principal = await _jwtTokenService.ValidateTokenAsync(JwtTokenType.RefreshToken, refreshToken);
            }
            catch (Exception ex)
            {
                _logger.LogError($"{nameof(RefreshToken)}: Exception: {ex.ToString()}");
                return(Result <JsonWebToken>(DefaultStatusCode.Fail));
            }

            var token = await _jwtTokenService.RefreshTokenAsync(refreshToken);

            return(Result(DefaultStatusCode.Success, token));
        }
        private static async Task TryRefreshTokenAsync(HttpContext httpContext, IJwtTokenService jwtTokenService, JwtOptions jwtOptions)
        {
            var refreshToken = httpContext.GetRefreshToken();

            if (string.IsNullOrEmpty(refreshToken))
            {
                return;
            }

            try
            {
                var principal = await jwtTokenService.ValidateTokenAsync(JwtTokenType.RefreshToken, refreshToken, jwtOptions);

                var jwtToken = await jwtTokenService.RefreshTokenAsync(refreshToken, jwtOptions);

                httpContext.Response.Headers.Add("access-token", jwtToken.AccessToken);
                httpContext.Response.Headers.Add("x-access-token", jwtToken.RefreshToken);
                httpContext.Response.Headers.Add("access-token-exp", jwtToken.AccessExpires.ToString());
                httpContext.Response.Headers.Add("x-access-token-exp", jwtToken.RefreshExpires.ToString());
            }
            catch
            {
            }
        }