"could not establish secure channel for ssl/tls with authority" 在特定服务器上调用 WCF 服务时

"could not establish secure channel for ssl/tls with authority" when calling WCF service on specific server

我们围绕 WCF API 服务进行了一些自动化测试,但我们希望能够在使用新服务器之前快速测试添加到我们的生产场的新服务器。我们目前以这种方式调用 ApiService:

using (var wcfApiServiceClient = new ApiServiceClient())
{
    WcfResponse = wcfApiServiceClient.Request(request);
}

但是请求失败并显示消息 Could not establish trust relationship for the SSL/TLS secure channel with authority 'servername:12345'. 当我尝试在浏览器中查看 wsdl 时,我收到一个不受信任的证书警告,所以我想这是我的问题。我在我的 app.config:

中尝试过这些东西
<endpoint ... bindingConfiguration="BasicHttpBinding_IApiService"  
              behaviorConfiguration="DisableServiceCertificateValidation" />
<behaviors>
  <endpointBehaviors>
    <behavior name="DisableServiceCertificateValidation">
      <clientCredentials>
        <serviceCertificate>
          <authentication certificateValidationMode="None"
                          revocationMode="NoCheck" />
        </serviceCertificate>
      </clientCredentials>
    </behavior>
  </endpointBehaviors>
</behaviors>

<system.net>
  <settings>
    <servicePointManager checkCertificateName="false" checkCertificateRevocationList="false" />
  </settings>
</system.net>

<basicHttpBinding>
    <binding name="BasicHttpBinding_IApiService">
      <security mode="Transport" >
        <transport clientCredentialType="None" proxyCredentialType="None" />
      </security>
    </binding>
</basicHttpBinding>

我无法更改服务器上的任何内容,但是在使用 .net 调用 WCF 服务时有什么方法可以忽略 ssl 错误吗?谢谢!

当您的 Api 客户端连接到服务器时,服务器正在发回证书,在这种情况下,该证书不受信任 => 握手失败。我还会尝试从浏览器下载证书,并使用 windows mmc.exe 工具将其导入客户端计算机的根证书颁发机构。

希望对您有所帮助:)

我找到了解决办法。添加这行代码解决了我的问题。我认为这是一个潜在的安全风险,因为我正在绕过证书警告,但由于我正在测试内部应用程序,所以我并不担心。

System.Net.ServicePointManager.ServerCertificateValidationCallback =
            (sender, certificate, chain, sslPolicyErrors) => true;