Skip to content

Instantly share code, notes, and snippets.

@xximjasonxx
Created December 28, 2019 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xximjasonxx/c28866938aa5b6d7485108378cfac000 to your computer and use it in GitHub Desktop.
Save xximjasonxx/c28866938aa5b6d7485108378cfac000 to your computer and use it in GitHub Desktop.
public TokenReadResult ReadToken(string tokenString)
{
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JwtKey"]));
var handler = new JwtSecurityTokenHandler();
var validations = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = key,
ValidateIssuer = true,
ValidIssuer = _configuration["JwtIssuer"],
ValidateAudience = true,
ValidAudience = _configuration["JwtAudience"]
};
var identity = handler.ValidateToken(tokenString, validations, out var tokenSecure).Identity as ClaimsIdentity;
if (identity == null)
{
throw new Exception("boom - Identity is not valid");
}
return new TokenReadResult
{
Username = identity.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment