我正在尝试使用RSASecurityKey在我的api服务中实现jwt auth,但是我无法创建错误代码为"WindowsCryptographicException: The requested operation is not supported"
的rsa对象
我正在使用 selfwriting 扩展方法从xml文件加载私钥和公钥:
internal static class RsaXmlExtensions
{
private const string NodeKey = "RSAKeyValue";
public static void LoadFromXml(this RSA rsa, string path)
{
var parameters = (object)new RSAParameters();
var paramType = parameters.GetType();
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(File.ReadAllText(path));
foreach (XmlNode node in xmlDocument[NodeKey].ChildNodes)
{
var field = paramType.GetField(node.Name);
if (field == null) continue;
var innerBytes = GetUnicodeBytes(node.InnerText);
field.SetValue(parameters, innerBytes);
}
rsa.ImportParameters((RSAParameters)parameters);
}
private static byte[] GetUnicodeBytes(string str) => Encoding.Unicode.GetBytes(str);
}
然后加载:
public static class SecurityKeyProvider
{
static SecurityKeyProvider()
{
var basePath = AppDomain.CurrentDomain.BaseDirectory;
PublicKey = LoadKey(basePath + KeysPaths.PublicKey);
PrivateKey = LoadKey(basePath + KeysPaths.PrivateKey);
}
public static RSA PublicKey { get; }
public static RSA PrivateKey { get; }
private static RSA LoadKey(string path)
{
using (var rsa = RSA.Create())
{
rsa.LoadFromXml(path);
return rsa;
}
}
}
我在做什么错了?