在Server 2012 R2上通过.net VB代码访问REST API会生成SSL/TLS错误

m0rkklqb  于 2022-11-14  发布在  .NET
关注(0)|答案(1)|浏览(121)

当我在woocommerce中访问REST API时,我收到此错误:-
“无法将数据写入传输连接:远程主机强制关闭了现有连接。”
或者
此错误:-
“请求被中止:无法创建SSL/TLS安全通道。”

请注意,我只在Windows 10计算机上的Server 2012 R2计算机上收到此错误,没有错误。

这是我在访问API之前的.net vb代码。
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12 Or SecurityProtocolType.Ssl3
上面的代码是否足够?
有数以千计的引用这个错误,但没有一个具体的答案。我甚至尝试使用一个程序称为IISCrypto,设置各种密码;没有结果。

qjp7pelc

qjp7pelc1#

您可能会发现以下内容很有用:https://www.hass.de/content/setup-microsoft-windows-or-iis-ssl-perfect-forward-secrecy-and-tls-12
此外-我挖了一些旧的代码,在Windows 2012 R2上使用,以启用TLS 1.2,见下文:

Dim bypass_ssl_validation As Boolean

      Dim security_protocols As System.Net.SecurityProtocolType

      ' Set the following flag to true to bypass SSL certificate validation; if any
      ' errors are encountered with SSL, they will be ignored.  This may need to be
      ' done in cases where a protocol mismatch error occurs or the remote servers
      ' SSL certificate has expired ...

      bypass_ssl_validation = True

      Try

         ' Get current security protocols ...

         security_protocols = System.Net.ServicePointManager.SecurityProtocol

         ' Set security protocol to TLS 1.2 only ...
         ' NOTE: If more than one protocol is enabled when we try to post,
         '       the call will fail with the following exception:
         '       System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.

         System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

         ' If flag set - bypass SSL Certificate Validation Checking.
         ' Note that this is a Shared property on the 'ServicePointManager' object, and must 
         ' then persist for the entire application, which we probably do not want -- so set 
         ' back to default behavior and end of try block ...

         If ( bypass_ssl_validation ) Then

            System.Net.ServicePointManager.ServerCertificateValidationCallback = AddressOf validate_remote_certificate

         End If

         ' POST data as needed ...

      Catch ex as Exception

         ' Log / report error ...

      Finally

         ' Restore previous protocols ...

         System.Net.ServicePointManager.SecurityProtocol = security_protocols

         ' If we bypassed SSL validation, restore default behavior ...

         If ( bypass_ssl_validation ) Then

            System.Net.ServicePointManager.ServerCertificateValidationCallback = Nothing

         End If

      End Try


   '-------------------------------------------------
   ' Delegate to ignore SSL Certificate errors if need
   ' be. Used when configuring ServicePointManager ...
   '
   Public Shared Function validate_remote_certificate( ByVal sender As Object, _
                                                       ByVal certificate As X509Certificate, _
                                                       ByVal chain As X509Chain, _
                                                       ByVal ssl_policy_errors As SslPolicyErrors ) As Boolean

      Return True

   End Function

相关问题