X509Certificate2 the server mode SSL must use a certificate with the associated private key

不羁的心 提交于 2019-12-07 17:23:23

问题


I use SslStream to build a web server. However, the code below throws an exception when AuthenticateAsServer.

static X509Certificate cert;
protected virtual Stream GetStream(TcpClient client)
{
        var ss = new SslStream(client.GetStream(), false);
        if (cert == null)
        {
            cert = X509Certificate2.CreateFromCertFile("test.cer");
        }
        ss.AuthenticateAsServer(cert, false, System.Security.Authentication.SslProtocols.Tls, true);
        return ss;
}

I've already used X509Certificate2 to load the cert file why it still throw the exception (The server mode SSL must use a certificate with the associated private key)?

The cert file was created using the following command:

makecert    
    -pe                                     Exportable private key
    -n "CN=localhost"                       Subject name
    -ss my                                  Certificate store name
    -sr LocalMachine                        Certificate store location
    -a sha1                                 Signature algorithm
    -sky signature                          Subject key type is for signature purposes
    -r                                      Make a self-signed cert
    "test.cer"                            Output filename

回答1:


makecert.exe -r -pe -n "CN=localhost" -sky exchange -sv server.pvk server.cer
pvk2pfx -pvk server.pvk -spc server.cer -pfx server.pfx -pi <password>

var certificate = new X509Certificate("path\server.pfx", "password");


来源:https://stackoverflow.com/questions/31615062/x509certificate2-the-server-mode-ssl-must-use-a-certificate-with-the-associated

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