Search code examples
c#wcfssl

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


We have some automation testing surrounding our WCF API service, but we want to quickly be able to test new servers that get added to our production farm before we use them. We are currently calling the ApiService this way:

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

But the request failed with the message Could not establish trust relationship for the SSL/TLS secure channel with authority 'servername:12345'. When I try and view the wsdl in a browser, I get an untrusted cert warning, so I imagine that is my issue. I've tried these things in my 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>

I cannot change anything on the server, but is there some way to ignore ssl errors when calling a WCF service using .net? Thanks!


Solution

  • I was able to find a solution. Adding this line of code fixed my issue. I would imnagine this to be a potential security risk, seeing as I am bypassing the cert warning, but since I am testing internal applications I am not concerned.

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