Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尽管设置了ServerCertificateValidationCallback,但无法创建SSL/TLS安全通道_C#_Ssl_Https - Fatal编程技术网

C# 尽管设置了ServerCertificateValidationCallback,但无法创建SSL/TLS安全通道

C# 尽管设置了ServerCertificateValidationCallback,但无法创建SSL/TLS安全通道,c#,ssl,https,C#,Ssl,Https,我正在尝试使用自签名证书建立到测试服务器的SSL/TLS连接。通过不安全的渠道进行沟通没有问题 以下是我的示例代码,我是根据以下解决方案编写的: 还有这个 ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; 但每次我都有一个例外: InnerException: System.Net.Http.HttpRequestException _HResult=-2146233

我正在尝试使用自签名证书建立到测试服务器的SSL/TLS连接。通过不安全的渠道进行沟通没有问题

以下是我的示例代码,我是根据以下解决方案编写的:

还有这个

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: 

我做错了什么?为什么我无法连接到此服务器(该服务器具有无效的自签名证书)

您使用ServerCertificateValidationCallback操作正确。这不是你面临的问题。您面临的问题很可能是SSL/TLS协议的版本

例如,如果您的服务器仅提供SSLv3和TLSv10,而您的客户端需要TLSv12,则您将收到此错误消息。您需要做的是确保客户端和服务器都支持一个公共协议版本

当我需要一个能够连接到尽可能多的服务器(而不是尽可能安全)的客户机时,我使用这个(与设置验证回调一起使用):


作为对仍然遇到此问题的任何人的后续行动,我添加了ServicePointManager.SecurityProfile选项,如解决方案中所述:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
但我仍然收到相同的“请求被中止:无法创建SSL/TLS安全通道”错误。我试图用HTTPS SOAP API接口(即多年前安装的语音邮件、IP电话系统等)连接到一些较旧的语音服务器。这些仅支持SSL3连接,因为它们上次更新是在几年前

有人会认为,将SSl3列入安全协议列表会起到作用,但事实并非如此。我可以强制连接的唯一方法是只包括Ssl3协议,而不包括其他协议:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
然后连接发生了——对我来说似乎是一个bug,但直到最近我为这些服务器提供的工具才开始出现错误,这些工具已经存在多年了——我相信微软已经开始推出系统更改,更新这种行为,以强制TLS连接,除非没有其他更改另类


无论如何,如果您仍然在一些旧站点/服务器上遇到这种情况,那么值得一试。

我们今天已经解决了同样的问题,您所需要做的就是增加.NET的运行时版本

4.5.2在上述问题上对我们不起作用,而4.6.1则可以

如果需要保留.NET版本,请设置

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

如果您正在使用一个新域名,并且您已经完成了上述所有操作,但仍然收到相同的错误,请检查您是否清除了电脑上的DNS缓存。 更多细节

Windows®8

要在使用Windows 8时清除DNS缓存,请执行以下步骤:

在键盘上,按Win+X打开WinX菜单

在命令提示符上单击鼠标右键,然后选择“以管理员身份运行”

运行以下命令:

ipconfig/flushdns

如果命令成功,系统将返回以下消息:

Windows IP配置已成功刷新DNS解析程序缓存

Windows®7

要在使用Windows 7时清除DNS缓存,请执行以下步骤:

单击开始

在“开始”菜单搜索文本框中输入cmd

在命令提示符上单击鼠标右键,然后选择“以管理员身份运行”

运行以下命令:

ipconfig/flushdns

如果命令成功,系统将返回以下消息:
Windows IP配置已成功刷新DNS解析程序缓存。

移动此行:ServicePointManager.SecurityProtocol=SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12

在这一行之前:HttpWebRequest=(HttpWebRequest)WebRequest.Create(uri)


原始帖子:

我遇到了这个线程,因为我还遇到了一个错误无法创建SSL/TLS安全通道。在我的例子中,我试图使用
调用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 ...

希望我的经验可以帮助其他对这个问题有特殊见解的人。

在我的例子中,TLS1_2在客户端和服务器上都已启用,但服务器正在使用MD5,而客户端将其禁用。所以,在上测试客户端和服务器,或者使用openssl/s_客户端进行测试,以查看发生了什么。另外,请使用Wireshark检查所选密码。

TLS 1.0和1.1现在已经失效。Amazon web服务器上的一个包被更新,我们开始出现这个错误

答案在上面,但您不应该再使用
tls
tls11

特别是对于ASP.Net,请将其添加到启动方法之一

        public Startup()
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12;

但我相信类似的方法在许多其他情况下也会奏效。

我也有类似的问题。由于狮子狗攻击,我们被迫关闭服务器上的TLS1.0。当我这样做时,第三方应用程序停止工作。反编译assambly会显示与您类似的代码。它使用WebClient.SendAsync而不是GetAsync。如果我将代码复制到consoleapp进行测试,我可以复制错误,通过添加ServicePointManager.SecurityProtocol=SecurityProtocolType.Tls11,它可以工作。我认为在服务器和客户端之间会有某种握手,以发现要使用什么TLS版本?当1.0被禁用时,为什么它不自动选择1.1?谢谢!我为此挣扎了3个小时。添加这一行为我做到了:
ServicePointManag
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
$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 ...
        public Startup()
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12;