X509Certificate2.CreateFromCertFile() on .NET Core

扶醉桌前 提交于 2019-12-21 20:15:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!