Last active
July 22, 2019 14:43
-
-
Save ntakouris/51961156627da4c18a3137bbe5073618 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private ClaimsPrincipal GetPrincipalFromToken(string token) | |
| { | |
| var tokenHandler = new JwtSecurityTokenHandler(); | |
| try | |
| { | |
| var principal = tokenHandler.ValidateToken(token, _tokenValidationParameters, out var validatedToken); | |
| if (!IsJwtWithValidSecurityAlgorithm(validatedToken)) | |
| { | |
| return null; | |
| } | |
| return principal; | |
| } | |
| catch | |
| { | |
| return null; | |
| } | |
| } | |
| private bool IsJwtWithValidSecurityAlgorithm(SecurityToken validatedToken) | |
| { | |
| return (validatedToken is JwtSecurityToken jwtSecurityToken) && | |
| jwtSecurityToken.Header.Alg.Equals(SecurityAlgorithms.HmacSha256, | |
| StringComparison.InvariantCultureIgnoreCase); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment