问题
I am trying to use X509Certificate2.CreateFromCertFile()
method in an ASP.NET Core application, but it seems like it's not supported for .NET Core.
Is there an alternative way of creating an X509Certificate2
object from a cert/pfx file?
回答1:
Maybe something like this?
public Certificate CreateFromCertFile()
{
string cerFile = @"c:\cer.txt";
string keyFile = @"c:\key.txt";
var cert = new X509Certificate2(cerFile);
cert.PrivateKey = CreateRSAFromFile(keyFile);
return cert;
}
private RSACryptoServiceProvider CreateRSAFromFile(string filename)
{
byte[] pvk = null;
using (var fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
pvk = new byte[fs.Length];
fs.Read(pvk, 0, pvk.Length);
}
var rsa = new RSACryptoServiceProvider();
rsa.ImportCspBlob(pvk);
return rsa;
}
来源:https://stackoverflow.com/questions/35845076/x509certificate2-createfromcertfile-on-net-core