我试图使用SslStream类连接到https服务器(服务器使用受信任的根证书),但是当我使用SslStream.AuthenticateAsClient(字符串、X509CertificateCollection、SslProtocols、布尔值)时,程序无穷无尽地站在那里,没有抛出任何异常或正在进行。以下是代码:
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文件更新了代码
发布于 2018-02-15 16:33:05
因为您试图在没有私钥的情况下(在cerName变量中)执行客户端证书身份验证。您应该使用有效的私钥引用X509Certificate2对象。无论是从PKCS#12/PFX文件还是从个人商店。
更新:
您需要将server.crt和server.key合并到PFX文件中。将两个文件放在同一个文件夹中(并确保两个名称具有相同的名称)并运行certutil命令:
certutil -mergepfx path\server.crt path\server.pfx其中path\server.crt是.crt文件的实际路径,path\server.pfx是保存结果PFX的路径。当出现提示时,输入PFX的密码。然后使用X509Certificate2 (String, SecureString)构造函数获取代码中带有私钥的证书。
https://stackoverflow.com/questions/48811727
复制相似问题