X509Certificate2.CreateFromCertFile() 在 .NET Core 上

X509Certificate2.CreateFromCertFile() on .NET Core

我正在尝试在 ASP.NET Core 应用程序中使用 X509Certificate2.CreateFromCertFile() 方法,但 .NET Core 似乎不支持它。

是否有从 cert/pfx 文件创建 X509Certificate2 对象的替代方法?

也许是这样的?

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;
}