一直在寻找各地的答案,但似乎找不到适用于我的答案。问题是我试图用c#中的ASP.NET构建一个JWT令牌。我遇到的问题是它在某处增加了一个" nbf"自动声明我的声明,我似乎无法弄清楚如何删除它,因为API主机不允许它在令牌中。这里有一段代码,其中包含了创建令牌的内容:
var plainTextSecurityKey = "key";
var signingKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(
Encoding.UTF8.GetBytes(plainTextSecurityKey));
var signingCredentials = new Microsoft.IdentityModel.Tokens.SigningCredentials(signingKey,
Microsoft.IdentityModel.Tokens.SecurityAlgorithms.HmacSha256Signature);
var claimsIdentity = new ClaimsIdentity(new List<Claim>()
{
new Claim("iss", "smthing"),
new Claim("sub", "smthing"),
new Claim("iat", ToUnixTime(issued).ToString()),
new Claim("exp",ToUnixTime(expire).ToString()),
new Claim("aud", JsonConvert.SerializeObject(new[] { "ohlc" }).ToString())
});
claimsIdentity.TryRemoveClaim(claimsIdentity.FindFirst("nbf"));
var securityTokenDescriptor = new Microsoft.IdentityModel.Tokens.SecurityTokenDescriptor()
{
Subject = claimsIdentity,
SigningCredentials = signingCredentials,
};
securityTokenDescriptor.NotBefore = null;
var tokenHandler = new JwtSecurityTokenHandler();
var plainToken = tokenHandler.CreateToken(securityTokenDescriptor);
var signedAndEncodedToken = tokenHandler.WriteToken(plainToken);
我在形成claimIdentity后尝试删除nbf,但似乎没有在那里添加。
答案 0 :(得分:0)
试试这个:
tokenHandler.SetDefaultTimesOnTokenCreation = false;
参考:System.IdentityModel.Tokens.Jwt