我正在尝试使用自签名证书建立与测试服务器的 SSL/TLS 连接。通过不安全通道的通信没有出现任何问题。
这是我的示例代码,是我根据此解决方案编写的:允许使用 HttpClient 不受信任的 SSL 证书 C# 忽略证书错误? .NET 客户端连接到 ssl Web API
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
var c = new HttpClient();
var r = c.GetAsync("https://10.3.0.1:8443/rest/v1").Result;
if (r.IsSuccessStatusCode)
{
Log.AddMessage(r.Content.Get<string>());
}
else
{
Log.AddMessage(string.Format("{0} ({1})", (int)r.StatusCode, r.ReasonPhrase));
}
也尝试过这个:
var handler = new WebRequestHandler();
handler.ServerCertificateValidationCallback = delegate { return true; };
var c = new HttpClient(handler);
...
还有这个
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
但每次我都会遇到例外:
InnerException: System.Net.Http.HttpRequestException
_HResult=-2146233088
_message=An error occurred while sending the request.
HResult=-2146233088
IsTransient=false
Message=An error occurred while sending the request.
InnerException: System.Net.WebException
_HResult=-2146233079
_message=The request was aborted: Could not create SSL/TLS secure channel.
HResult=-2146233079
IsTransient=false
Message=The request was aborted: Could not create SSL/TLS secure channel.
Source=System
StackTrace:
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
InnerException:
我做错了什么?为什么我无法连接到该服务器(其具有无效的自签名证书)
例如,如果您的服务器仅提供 SSLv3 和 TLSv10,而您的客户端需要 TLSv12,那么您将收到此错误消息。您需要做的是确保客户端和服务器都支持通用协议版本。
当我需要一个能够连接到尽可能多的服务器(而不是尽可能安全)的客户端时,我使用它(与设置验证回调一起):
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
4.5.2 对我们遇到上述问题不起作用,而 4.6.1 还可以
如果需要保留.NET版本,则设置
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
但我仍然收到相同的“请求已中止:无法创建 SSL/TLS 安全通道”错误。我尝试使用 HTTPS SOAP API 接口连接到一些较旧的语音服务器(即多年前安装的语音邮件、IP 电话系统等)。这些仅支持 SSL3 连接,因为它们上次更新是在几年前。
有人可能认为将 SSl3 包含在安全协议列表中就可以解决问题,但事实并非如此。我强制连接的唯一方法是仅包含 Ssl3 协议而不包含其他协议:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
然后连接就通过了——对我来说似乎是一个错误,但直到最近我为这些服务器提供的工具才开始抛出错误,这些工具已经存在多年了——我相信微软已经开始推出已经更新的系统更改除非没有其他选择,否则此行为会强制 TLS 连接。
无论如何 - 如果您仍然在某些旧站点/服务器上遇到此问题,那么值得一试。
var client = new HttpClient(new HttpClientHandler { SslProtocols = SslProtocols.Tls12});
http://ssllabs.com 上测试客户端和服务器,或使用 openssl/s_client 进行测试,看看发生了什么。另外,使用 Wireshark 检查所选密码。
答案在上面,但你不应该再使用
tls
或
tls11
了。特别是对于 ASP.Net,请将其添加到您的启动方法之一。
public Startup()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12;
但我确信类似的方法在许多其他情况下也适用。
无法创建 SSL/TLS 安全通道。在我的例子中,我尝试使用 Invoke-RestMethod
从 PowerShell 访问 Siebel 配置 REST API,但上面的建议都没有帮助了。最终我偶然发现了问题的原因:我联系的服务器需要客户端证书身份验证。
为了使通话正常进行,我必须提供带有
-Certificate
参数的客户端证书(包括私钥):
$Pwd = 'certificatepassword'
$Pfx = New-Object -TypeName 'System.Security.Cryptography.X509Certificates.X509Certificate2'
$Pfx.Import('clientcert.p12', $Pwd, 'Exportable,PersistKeySet')
Invoke-RestMethod -Uri 'https://your.rest.host/api/' -Certificate $Pfx -OtherParam ...
希望我的经验可以帮助其他对这个问题有特殊看法的人。
try {
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = delegate {
return true;
};
var webClient = new WebClient();
var s = webClient.DownloadString("https://google.com");
Console.WriteLine(s);
} catch (Exception ex) {
Console.WriteLine(ex);
}
Console.ReadLine();
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;