Как я могу создать RsaSecurityKey из пары открытых / закрытых ключей. Мне нужно создать токен идентификатора JWT
Мой образец пары ключ-значение приведен в методе
public string GetIdTokenStringNew(Dictionary<string, object> inputClaims, string publicKey, string privateKey )
{
string result = null;
try
{
var tokenHandler = new JwtSecurityTokenHandler();
publicKey = @"-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANPCGYnVEa1jQPMSHXST8NVIrcAYZcWr
..............
-----END PUBLIC KEY-----
";
privateKey = @"-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIBrzBJBgkqhkiG9w0BBQ0wPDAbBgkqhkiG9w0BBQwwDgQIa3E4RUhvGGwCAggA
MB0GCWCGSAFlAwQBKgQQnfLhTMhpN7BE0A+viaWeWwSCAWD2yFBSGAP6boVzCOqg
41IoRHrZHgTQVbySuruav5nM3eMe3psHD0C4Tbyj4av3UnD2/ebZz8f9IiObJ45a
................................................................
....
-----END ENCRYPTED PRIVATE KEY-----";
List<Claim> claims = new List<Claim>();
foreach (var o in inputClaims)
{
string val = null;
if (o.Value != null)
{
Type t = o.Value.GetType();
bool isDict = t.IsGenericType /*&& t.GetGenericTypeDefinition() == typeof(Dictionary<,>)*/;
if (isDict)
{
val = JsonSerializer.Serialize(o.Value);
}
else
{
val = o.Value.ToString();
}
}
claims.Add(new Claim(o.Key, val));
}
var rsaParameters = new RSAParameters();// it should be from public /private key
var securitykey = new RsaSecurityKey(rsaParameters);
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(claims),
Expires = DateTime.UtcNow.AddSeconds(60 * 5),
SigningCredentials = new SigningCredentials(securitykey, SecurityAlgorithms.RsaSha256),
Audience = "....",
Issuer = "..."
};
var additionalheader = new Dictionary<string, object>
{
{ "kid", "***" }
};
tokenDescriptor.AdditionalHeaderClaims = additionalheader;
var token = tokenHandler.CreateToken(tokenDescriptor);
if (token != null && token is JwtSecurityToken)
{
result = (token as JwtSecurityToken).RawData;
}
}
catch (Exception ex)
{
}
return result;
}