这里是一个在 .net core 中可行的示例:
使用 openssl 工具。打开终端并输入以下命令:
openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -outform PEM -pubout -out public.pem
这将会创建2个文件。
在我们能够在.NET Core应用程序中使用RSA密钥之前,还有一件事情需要做。我们需要将它们转换为XML格式。使用"RSA PEM to XML Converter"进行转换。
可以在此处完成:在线RSA密钥转换器
在将XML复制到private-rsa-key.xml和public-rsa-key.xml文件之前,使用XML格式化工具进行格式化。
私钥仅适用于负责生成令牌的服务。不能在项目之外共享该密钥。
Startup.cs :
public class Startup
{
public IConfiguration Configuration { get; }
private SigningCredentials _signingCredentials;
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services
.AddIdentityServer()
.AddSigningCredential(_signingCredentials)
.AddInMemoryApiResources(_identityConfig.GetApiResources())
.AddInMemoryClients(_identityConfig.GetClients());
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseMvc();
app.UseIdentityServer();
}
private void InitializeRsaKey()
{
try
{
RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(2048);
var rsaParametersPrivate = RSAExtensions.RSAParametersFromXmlFile(Configuration.GetSection("JwtSettings:rsaPrivateKeyXml").Value);
rsaProvider.ImportParameters(rsaParametersPrivate);
var securityKey = new RsaSecurityKey(rsaProvider);
_signingCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.RsaSha256);
}
catch(Exception ex)
{
throw new Exception("Identity Server RSA Key initialization failed. " + ex.ToString());
}
}
}
RSAExtensions类:
public static class RSAExtensions
{
public static RSAParameters RSAParametersFromXmlFile(string xmlFilePath)
{
RSAParameters parameters = new RSAParameters();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(File.ReadAllText(xmlFilePath));
if (xmlDoc.DocumentElement.Name.Equals("RSAKeyValue"))
{
foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
{
switch (node.Name)
{
case "Modulus": parameters.Modulus = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
case "Exponent": parameters.Exponent = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
case "P": parameters.P = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
case "Q": parameters.Q = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
case "DP": parameters.DP = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
case "DQ": parameters.DQ = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
case "InverseQ": parameters.InverseQ = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
case "D": parameters.D = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
}
}
}
else
{
throw new Exception("Invalid XML RSA key.");
}
return parameters;
}
}
在 appsettings.json 中添加以下内容:
"JwtSettings": {
"rsaPrivateKeyXml": "RSAKeys/private-rsa-key.xml",
},
我不确定您是否可以在appsettings.json中存储原始的XML。如果不能,您可以编写一个帮助方法将pem转换为xml,并将原始的pem内容存储在配置变量中。
我在GitHub上找到了一个示例项目,可能会对您进行转换有所帮助:Rsa-Dotnet-Core
pfxBytes
。 - pepo