"Could not create SSL/TLS secure channel" 使用 X.509 对 Web 服务进行身份验证时出错
"Could not create SSL/TLS secure channel" error when authenticating to a web service using X.509
我已经看到几个线程出现相同的错误并尝试了各种可能的解决方案,但到目前为止还没有成功。我正在使用客户端的 REST 端点,它需要客户端证书进行身份验证。我有证书并通过双击 .pfx 并完成向导为当前用户和本地计算机安装了它。我还 运行 使用 winhttpcertcfg
以下内容,以便应用程序池身份帐户 (Network Service
) 可以使用证书:
winhttpcertcfg -i "<path to .pfx> -c LOCAL_MACHINE\My -a "Network Service" -p "<password for .pfx>
我已经将证书管理单元添加到 MMC 并添加了我的证书,所以当我点击 Chrome 中的 API 端点 URL 时,证书出现了,我单击它,我看到返回 XML。
不幸的是,我使用的代码在尝试获得响应时遇到错误。我的代码(在 MVC 操作中):
var url = "https://obfuscated.api.endpoint.host/api/Profile";
var certPath = @"C:\Users\paldrich\AdvisorDirectoryClient.pfx";
var xmlResult = string.Empty;
try
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12;
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.ContentType = "application/xml;charset=UTF8";
X509Certificate cert = new X509Certificate(certPath, "obfuscated-password");
request.ClientCertificates.Add(cert);
request.PreAuthenticate = true;
var response = request.GetResponse();
var responseStream = response.GetResponseStream();
xmlResult = new StreamReader(responseStream).ReadToEnd();
}
catch (Exception ex)
{
Log.Error("Bio App web service call failed.", ex, "AdvisorController");
xmlResult = ex.Message;
}
return Json(xmlResult, JsonRequestBehavior.AllowGet);
当它尝试获取响应 (response.GetResponse()
) 时,我看到以下错误:The request was aborted: Could not create SSL/TLS secure channel.
我尝试过的事情:
- G运行让
Network Service
完全访问我在 C:\Users\paldrich
下的证书。
- 使
ServicePointManager.SecurityProtocol
只是 SecurityProtocolType.Tls12
也只是 SecurityProtocolType.Ssl3
.
- 正在当前用户和本地计算机的证书导入向导中检查 "Mark this key as exportable"。
- 已验证我已
Use Tls 1.0
、Use Tls 1.1
和 Use Tls 1.2
都已在 Internet 属性中选中。
提交后我意识到我的问题是什么。我需要使用 X509Certificate2
而不是 X509Certificate
(我通过尝试 2 弄清楚了这一点),尽管我不明白为什么,除了 X509Certificate2 更新的事实之外。无论如何,XML 即将到来;它显示为 Unicode 转换后的字符串,但总的来说目标已经完成。
所以,而不是:
X509Certificate cert = new X509Certificate(certPath, "obfuscated-password");
我必须使用:
X509Certificate2 cert = new X509Certificate2(certPath, "obfuscated-password");
我已经看到几个线程出现相同的错误并尝试了各种可能的解决方案,但到目前为止还没有成功。我正在使用客户端的 REST 端点,它需要客户端证书进行身份验证。我有证书并通过双击 .pfx 并完成向导为当前用户和本地计算机安装了它。我还 运行 使用 winhttpcertcfg
以下内容,以便应用程序池身份帐户 (Network Service
) 可以使用证书:
winhttpcertcfg -i "<path to .pfx> -c LOCAL_MACHINE\My -a "Network Service" -p "<password for .pfx>
我已经将证书管理单元添加到 MMC 并添加了我的证书,所以当我点击 Chrome 中的 API 端点 URL 时,证书出现了,我单击它,我看到返回 XML。
不幸的是,我使用的代码在尝试获得响应时遇到错误。我的代码(在 MVC 操作中):
var url = "https://obfuscated.api.endpoint.host/api/Profile";
var certPath = @"C:\Users\paldrich\AdvisorDirectoryClient.pfx";
var xmlResult = string.Empty;
try
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12;
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.ContentType = "application/xml;charset=UTF8";
X509Certificate cert = new X509Certificate(certPath, "obfuscated-password");
request.ClientCertificates.Add(cert);
request.PreAuthenticate = true;
var response = request.GetResponse();
var responseStream = response.GetResponseStream();
xmlResult = new StreamReader(responseStream).ReadToEnd();
}
catch (Exception ex)
{
Log.Error("Bio App web service call failed.", ex, "AdvisorController");
xmlResult = ex.Message;
}
return Json(xmlResult, JsonRequestBehavior.AllowGet);
当它尝试获取响应 (response.GetResponse()
) 时,我看到以下错误:The request was aborted: Could not create SSL/TLS secure channel.
我尝试过的事情:
- G运行让
Network Service
完全访问我在C:\Users\paldrich
下的证书。 - 使
ServicePointManager.SecurityProtocol
只是SecurityProtocolType.Tls12
也只是SecurityProtocolType.Ssl3
. - 正在当前用户和本地计算机的证书导入向导中检查 "Mark this key as exportable"。
- 已验证我已
Use Tls 1.0
、Use Tls 1.1
和Use Tls 1.2
都已在 Internet 属性中选中。
提交后我意识到我的问题是什么。我需要使用 X509Certificate2
而不是 X509Certificate
(我通过尝试 2 弄清楚了这一点),尽管我不明白为什么,除了 X509Certificate2 更新的事实之外。无论如何,XML 即将到来;它显示为 Unicode 转换后的字符串,但总的来说目标已经完成。
所以,而不是:
X509Certificate cert = new X509Certificate(certPath, "obfuscated-password");
我必须使用:
X509Certificate2 cert = new X509Certificate2(certPath, "obfuscated-password");