我正在使用Auth0和JWT对我的应用程序实施身份验证。我的所有声明名称都是大写格式,但是一旦JwtSecurityTokenHandler将我的标记转换为JwtSecurityToken,所有JwtSecurityToken的声明类型都会给出小写类型。
索赔& BuildToken
private string BuildToken(UserModel user)
{
var claims = new[] {
new Claim(JwtRegisteredClaimNames.Sub, user.Name),
new Claim(JwtRegisteredClaimNames.Email, user.Email),
new Claim(JwtRegisteredClaimNames.Birthdate, user.Birthdate.ToString("yyyy-MM-dd")),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
};
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Jwt:Key"]));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(_config["Jwt:Issuer"],
_config["Jwt:Issuer"],
claims,
expires: DateTime.Now.AddMinutes(30),
signingCredentials: creds);
return new JwtSecurityTokenHandler().WriteToken(token);
}
JwtSecurityTokenHandler
var handler = new JwtSecurityTokenHandler();
var tokenS = handler.ReadToken(context.HttpContext.Request.Headers["Authorization"]) as JwtSecurityToken;
var jti = tokenS.Claims.First(claim => claim.Type == "Sub").Value; //here Sub is giving exception, but if i change it to lower case sub, working fine.