gpt4 book ai didi

c# - 如何从 RSA 私钥在 jwt 中设置 kid header 声明?

转载 作者:行者123 更新时间:2023-12-05 04:41:18 24 4
gpt4 key购买 nike

我想生成一个带有“kid” header 声明的 JWT token 。我有一个 XML 格式的 RSA 私钥来签署 JWT token 。但是在我的 JWT 中,我找不到“kid” header 声明以及类型和算法。我该怎么做?

这是生成 JWT token 的代码:

public async Task<IActionResult> Generate()
{
var rsa = RSA.Create();
string key = await System.IO.File.ReadAllTextAsync(options.PrivateKeyFilePath);
rsa.FromXmlString(key);

var credentials = new SigningCredentials(new RsaSecurityKey(rsa), SecurityAlgorithms.RsaSha256);

var jwt = new JwtSecurityToken(
new JwtHeader(credentials),
new JwtPayload(
"webapi",
"webapi",
new List<Claim>(),
DateTime.UtcNow,
DateTime.UtcNow.AddHours(3)
)
);

string token = new JwtSecurityTokenHandler().WriteToken(jwt);

return Ok(new { Token = token });
}

这是我的 RSA 私钥。

<RSAKeyValue>
<Modulus>yRfxNTDYqxjEgow6HHBPBEiK6NrVCyLFpG8dklP7f7kFuKZozHopqnly/24Gf6jt9xYLFIsQhhRcuclzEKNnBcWzKlXg9xRwk0o2JzPCh1Ifn1XQ67FrD8+HlBT9DfxjkvzCkPLxi8UWxgifIGauVeFbhIOkVfS0JrIJyQI33sUmiciBGXnO9CjEUpiBcoY53CRa49aUBoKJFDuHV2zuPWCEHLYXrP8Ns15jRU70V/YYUzU3R3PnWk3ZA/12YqtMAJaXFE33DQE71Ccd6HsXfUJpJAM81O/pDPDsk3b3260eN20nLDT0F2acOYQb/3bVKzqZ97isZYqekkmXdeuy4Q==</Modulus>
<Exponent>AQAB</Exponent>
<P>5iuGQcTqCvpwII6EOr1+F98GviZ/PWtHoDkiP8ZiSVCH8XEYCiuPmuWBtOYlv+hLJ9zWUVPkD5uIatLttT6ZxCi7oP+A6htgTbRyLN4NAibwtfQAKQOtue98HyIE6J17OPu8EVBXUSL9rC98OxcbxqDPLOX0geWoIt8BIE9v5js=</P>
<Q>36kW0+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答案

kid 可以用 SecurityKey.KeyId 设置属性:

...
var rsaSecurityKey = new RsaSecurityKey(rsa);
rsaSecurityKey.KeyId = "your kid";
var credentials = new SigningCredentials(rsaSecurityKey, SecurityAlgorithms.RsaSha256);
...

有了这个,生成的 token 的第一部分是 Base64url 解码:

{"alg":"RS256","kid":"your kid","typ":"JWT"}

关于c# - 如何从 RSA 私钥在 jwt 中设置 kid header 声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70107544/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com