首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SslStream.AuthenticateAsClient不起作用

SslStream.AuthenticateAsClient不起作用
EN

Stack Overflow用户
提问于 2018-02-15 16:18:11
回答 1查看 3.9K关注 0票数 1

我试图使用SslStream类连接到https服务器(服务器使用受信任的根证书),但是当我使用SslStream.AuthenticateAsClient(字符串、X509CertificateCollection、SslProtocols、布尔值)时,程序无穷无尽地站在那里,没有抛出任何异常或正在进行。以下是代码:

代码语言:javascript
复制
String serverName = "https://192.168.32.74/params.cgi";
String pfxName = @"C:\...\server.pfx";

X509Certificate2 certificate = new X509Certificate2(pfxName, "mypassword");
//Create new X509 store called teststore from the local certificate store.
X509Store store = new X509Store("teststore", StoreLocation.CurrentUser);

//Create a collection and add two of the certificates.
X509Certificate2Collection collection = new X509Certificate2Collection();
collection.Add(certificate);

store.Open(OpenFlags.ReadWrite);
store.AddRange(collection);
try
{
    TcpClient client = new TcpClient();
        client.Connect(server, port);
        SslStream sslStream = new SslStream(
            client.GetStream(),
            false,
            new RemoteCertificateValidationCallback(ValidateServerCertificate),
            null);

        sslStream.AuthenticateAsClient(
            serverName,
            collection,
            SslProtocols.Default,
            true);
}
catch (Exception ex)
{
    Console.WriteLine("Exception: {0}", ex.ToString());
}
...

有人知道我为什么会有这个问题吗?

更新:

我已经用.pfx文件更新了代码

EN

回答 1

Stack Overflow用户

发布于 2018-02-15 16:33:05

因为您试图在没有私钥的情况下(在cerName变量中)执行客户端证书身份验证。您应该使用有效的私钥引用X509Certificate2对象。无论是从PKCS#12/PFX文件还是从个人商店。

更新:

您需要将server.crtserver.key合并到PFX文件中。将两个文件放在同一个文件夹中(并确保两个名称具有相同的名称)并运行certutil命令:

代码语言:javascript
复制
certutil -mergepfx path\server.crt path\server.pfx

其中path\server.crt是.crt文件的实际路径,path\server.pfx是保存结果PFX的路径。当出现提示时,输入PFX的密码。然后使用X509Certificate2 (String, SecureString)构造函数获取代码中带有私钥的证书。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48811727

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档