当我在 IE(工具/Internet 选项/内容/证书)中手动导入证书时,我的 C#.NET SSL 连接有效,但是如何通过代码加载证书?这是我的代码:
TcpClient client = new TcpClient(ConfigManager.SSLSwitchIP, Convert.ToInt32(ConfigManager.SSLSwitchPort));
SslStream sslStream = new SslStream(
client.GetStream(),
false,
new RemoteCertificateValidationCallback(ValidateServerCertificate),
null
);
sslStream.AuthenticateAsClient("Test");
如果我在 Internet Explorer 中手动导入我的证书文件,上面的代码可以正常工作。但是,如果我从 IE 中删除我的证书并改用以下代码,我会得到身份验证异常:
sslStream.AuthenticateAsClient("Test", GetX509CertificateCollection(), SslProtocols.Default, false);
这是'GetX509CertificateCollection'方法:
public static X509CertificateCollection GetX509CertificateCollection()
{
X509Certificate2 certificate1 = new X509Certificate2("c:\\ssl.txt");
X509CertificateCollection collection1 = new X509CertificateCollection();
collection1.Add(certificate1);
return collection1;
}
我应该怎么做才能动态加载我的证书?