【发布时间】:2016-11-03 17:04:13
【问题描述】:
我正在尝试使用相互证书身份验证与外部网站通信,但收到“无法为具有权限的 SSL/TLS 建立安全通道”异常。我已将其范围缩小到以下 sn-p(已删除敏感内容):
void Example()
{
string KeyIdentifier = "<MyKeyId>";
X509Store store = new X509Store("My", StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certificates = store.Certificates.Find(X509FindType.FindByThumbprint, KeyIdentifier, true);
X509Certificate2 certificate = certificates[0];
HttpWebRequest r = (HttpWebRequest)HttpWebRequest.Create("https://www.example.org");
r.ClientCertificates.Add( certificate);
r.Accept = "text/xml";
r.Method = WebRequestMethods.Http.Post;
string result = null;
using (HttpWebResponse resp = (HttpWebResponse)r.GetResponse())
{
StreamReader reader = new StreamReader(resp.GetResponseStream());
result = reader.ReadToEnd();
}
}
我已经正确设置了证书等,并且此代码成功找到了它们,使用 certificate.HasPrivateKey=true,但失败了。 SoapUI 愉快地与 TLS 连接。当我访问该网站时,Chrome 不会报告任何证书错误。但是,上面的代码总是抛出异常。我已经尝试了所有 3 个 TLS 版本,但均未成功。使用 ServicePointManager 跳过服务器证书验证没有区别。
奇怪的是,如果我指定 SSL3
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
然后该过程成功完成,但我自然不想使用 SSL3。该应用是在 Server 2012 上运行的 .Net 4.6。
【问题讨论】:
-
最早支持的.NET版本是4.5.2,也加入了TLS 1.2支持
-
我已将我的测试应用升级到 .Net 4.6,但仍然遇到上述问题。 TLS 继续抛出异常,而 SSL3 继续正常工作。
-
使用日志记录跟踪事务:ferozedaud.blogspot.com/2009/08/tracing-with-systemnet.html。日志文件应向您显示失败时发生的情况。
标签: c# ssl httpwebrequest client-certificates