/// <summary> /// Reads string token and creates same token with new valid lifetime. /// If currentToken is not valid, returns null. /// </summary> public JwtToken Refresh(string currentToken) { JwtClaimsPrincipalValidator validator = new JwtClaimsPrincipalValidator(Options); SecurityToken validatedToken; ClaimsPrincipal principal = validator.GetToken(currentToken, out validatedToken); if (principal == null || validatedToken == null) { return(null); } //creates claim list Claim idClaim = principal.Claims.FirstOrDefault(x => x.Type == JwtRegisteredClaimNames.Jti); Claim usernameClaim = principal.Claims.FirstOrDefault(x => x.Type == JwtRegisteredClaimNames.Sub); if (idClaim == null || usernameClaim == null) { return(null); } List <Claim> claims = new List <Claim>(); foreach (Claim claim in principal.Claims) { if (claim.Type != JwtRegisteredClaimNames.Jti && claim.Type != JwtRegisteredClaimNames.Sub) { claims.Add(claim); } } JwtToken refreshToken = Create(idClaim.Value, usernameClaim.Value, claims); return(refreshToken); }
/// <summary> /// Reads token from token string and creates ClaimsPrincipals if the token is valid. /// Token string must be in "Scheme xxx.." form such as "Bearer xxx" /// </summary> public ClaimsPrincipal Read(string token) { JwtClaimsPrincipalValidator validator = new JwtClaimsPrincipalValidator(Options); return(validator.Get(token)); }
/// <summary> /// Reads token from Request and creates ClaimsPrincipals if the token is valid /// </summary> public ClaimsPrincipal Read(HttpRequest request) { JwtClaimsPrincipalValidator validator = new JwtClaimsPrincipalValidator(Options); return(validator.Get(request)); }