我知道。我知道。很多问题都与这个确切的标题有关。但在我看过的所有这些中,我找不到一个使用 List<Claim>
、issuer
和 来创建 JWT audience
。
private string GetTokenString()
{
var claims = new List<Claim>()
{
new Claim("claim1", "foo-anything"),
new Claim("claim2", "bar-anything")
};
string keyValue = "1234567890qwertyuiopasdfghjklzxcvbnm"; // NOT THE REAL KEY (changed for this SO question)
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(keyValue));
var jwtToken = new JwtSecurityToken
(
issuer: "https://xxxxxx.net",
audience: "https://www.xxxxxx.com",
claims: claims,
signingCredentials: new SigningCredentials(key, SecurityAlgorithms.HmacSha256Signature),
expires: DateTime.Now.AddMinutes(30)
);
var handler = new JwtSecurityTokenHandler();
string tokenString = handler.WriteToken(jwtToken);
return tokenString;
}
然后,如果我将 tokenString 插入 https://jwt.io/,我获取
签名无效
我创建 JWT 的方式有问题吗?
萧十郎
相关分类