X509Certificate2服务器模式SSL必须使用带有关联私钥的证书 [英] X509Certificate2 the server mode SSL must use a certificate with the associated private key

查看:681
本文介绍了X509Certificate2服务器模式SSL必须使用带有关联私钥的证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SslStream来构建Web服务器.但是,下面的代码在AuthenticateAsServer时引发异常.

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

我已经使用X509Certificate2加载证书文件,为什么它仍然引发异常(服务器模式SSL必须使用带有关联私钥的证书)?

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

推荐答案

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");

这篇关于X509Certificate2服务器模式SSL必须使用带有关联私钥的证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆