我需要使用 C# 生成 JSON Web Key (jwk),如图所示。我已经阅读了几篇文章,但也有其他语言(例如 Java)的实现,并且有第三方库(Nimbus JOSE+JWT 库等)。您能帮我用 C# 生成 JSON Web Key 吗?
{
"e": "AQAB",
"n": "nZD7QWmIwj-3N_RZ1qJjX6CdibU87y2l02yMay4KunambalP9g0fU9yZLwLX9WYJINcXZDUf6QeZ-SSbblET-h8Q4OvfSQ7iuu0WqcvBGy8M0qoZ7I-NiChw8dyybMJHgpiP_AyxpCQnp3bQ6829kb3fopbb4cAkOilwVRBYPhRLboXma0cwcllJHPLvMp1oGa7Ad8osmmJhXhM9qdFFASg_OCQdPnYVzp8gOFeOGwlXfSFEgt5vgeU25E-ycUOREcnP7BnMUk7wpwYqlE537LWGOV5z_1Dqcqc9LmN-z4HmNV7b23QZW4_mzKIOY4IqjmnUGgLU9ycFj5YGDCts7Q",
"alg": "RS256",
"kid": "8f796169-0ac4-48a3-a202-fa4f3d814fcd",
"kty": "RSA",
"use": "sig"
}
Run Code Online (Sandbox Code Playgroud)
Christos 的上述评论指出了正确的方向,但是您真正想要的类是 JsonWebKeyConverter。在 Microsoft.IdentityModel.Tokens nuget 包中。
假设您的公钥文件在某处可用,那么您需要以下代码:
var cert = new X509Certificate2(@".\CertPublicKey.cer");
var key = new X509SecurityKey(cert);
var jwk = JsonWebKeyConverter.ConvertFromX509SecurityKey(key, true);
Run Code Online (Sandbox Code Playgroud)
首先创建两个 RSA 密钥:
RSA rsa1 = RSA.Create(2048);
RSA rsa2 = RSA.Create(2048);
RsaSecurityKey publicKey1 = new(rsa1.ExportParameters(false))
{
KeyId = "keyId1"
};
RsaSecurityKey publicKey2 = new(rsa2.ExportParameters(false))
{
KeyId = "keyId2"
};
RsaSecurityKey publicAndPrivateKey1 = new(rsa1.ExportParameters(true))
{
KeyId = "keyId1"
};
RsaSecurityKey publicAndPrivateKey2 = new(rsa2.ExportParameters(true))
{
KeyId = "keyId2"
};
Run Code Online (Sandbox Code Playgroud)
使用它们创建两个 JWK:
JsonWebKey jwk1 = JsonWebKeyConverter.ConvertFromRSASecurityKey(publicKey1);
JsonWebKey jwk2 = JsonWebKeyConverter.ConvertFromRSASecurityKey(publicKey2);
Run Code Online (Sandbox Code Playgroud)
将它们添加到 JWKS 并使用JsonExtensions.SerializeToJson()方法打印它(这里比 Newtonsoft JSON 效果更好,因为输出可以传递回JsonWebKey和JsonWebKeySet构造函数):
IList<JsonWebKey> jwksList = new List<JsonWebKey>
{
jwk1,
jwk2,
};
Dictionary<string, IList<JsonWebKey>> jwksDict = new()
{
{ "keys", jwksList }
};
string jwksStr = SerializeToJson(jwksDict);
Console.WriteLine(jwksStr); // put this at https://example.com/.well-known/jwks.json
JsonWebKeySet jwks = new(jwksStr);
Run Code Online (Sandbox Code Playgroud)
然后发出两个 JWT:
JsonWebTokenHandler tokenHandler = new();
SecurityTokenDescriptor descriptor1 = new()
{
Issuer = "example.com",
Audience = "cats",
SigningCredentials = new SigningCredentials(publicAndPrivateKey1, SecurityAlgorithms.RsaSsaPssSha256),
};
SecurityTokenDescriptor descriptor2 = new()
{
Issuer = "example.com",
Audience = "dogs",
SigningCredentials = new SigningCredentials(publicAndPrivateKey2, SecurityAlgorithms.RsaSsaPssSha256),
};
string jwt1 = tokenHandler.CreateToken(descriptor1);
string jwt2 = tokenHandler.CreateToken(descriptor2);
Run Code Online (Sandbox Code Playgroud)
最后,如果有人将这两个令牌返还给您,请验证它们:
TokenValidationParameters parameters = new()
{
ValidateIssuer = true,
ValidIssuer = "example.com",
ValidateAudience = true,
ValidAudiences = new[] { "cats", "mice" }, // "dogs" are not allowed
IssuerSigningKeys = jwks.GetSigningKeys(),
};
TokenValidationResult result1 = tokenHandler.ValidateToken(jwt1, parameters);
TokenValidationResult result2 = tokenHandler.ValidateToken(jwt2, parameters);
Console.WriteLine("jwt1 is valid: " + result1.IsValid);
Console.WriteLine("jwt2 is valid: " + result2.IsValid);
Run Code Online (Sandbox Code Playgroud)
我的控制台应用程序的屏幕截图:
这里是 .csproj 文件中使用的 Nuget 包:
<ItemGroup>
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.25.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.25.0" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
和using
声明:
using Microsoft.IdentityModel.Tokens;
using System.Security.Cryptography;
using static System.IdentityModel.Tokens.Jwt.JsonExtensions;
Run Code Online (Sandbox Code Playgroud)