我是JWT访问令牌生成方面的新手。我有公钥,私钥和ClientID。我需要生成Client_Assertion。
client_assertion: JWT (使用RS256作为签名算法,由客户机ID、公共证书和私钥签名)。
我已经找到了一些Node.JS代码,但是我想使用.Net框架(而不是.Net核心)来实现,Node.Js代码可以在此链接上看到
我需要用C#来做。从哪里开始,如何实现?
发布于 2022-03-14 01:45:09
我使用了.NET库
我还假设,正如您所说的,您有一个私钥,并且已经将其加载到一个RSACryptoServiceProvider中。
这是我的示例代码
首先,建立你的权利主张。这是您的JWT有效载荷
var claims = new Claim[]
{
new Claim(MicrosoftJwt.JwtRegisteredClaimNames.Sub, "your subject"),
new Claim(MicrosoftJwt.JwtRegisteredClaimNames.Iat, DateTime.Now.ToEpochSeconds().ToString(), ClaimValueTypes.Integer),
new Claim(MicrosoftJwt.JwtRegisteredClaimNames.Exp, DateTime.Now.AddMinutes(60).ToEpochSeconds().ToString(), ClaimValueTypes.Integer),
};然后,您需要使用私钥配置您的签名凭据。
// Assuming you already have your key loaded into an RSACryptoServiceProvider
var key = new MicrosoftTokens.RsaSecurityKey(csp)
var signingCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.RsaSha256);现在您可以创建您的令牌
var jwt = new JwtSecurityToken(
issuer : issuer,
audience : audience,
claims : claims,
signingCredentials: signingCredentials
);
// You can add extra items to your Header if necessary
jwt.Header.Add("kid", deviceId);然后,您可以将令牌写入字符串。
var assertion = new JwtSecurityTokenHandler().WriteToken(jwt);我认为assertion的价值是你想要得到的
https://stackoverflow.com/questions/67821344
复制相似问题