title: Add WCF custom certificate validation programatically
body: <p>I'm trying to access a WebService that requires a certificate issued by themselves, and I'm getting errors during the validation.</p>
  
  <pre><code>BasicHttpsBinding _binding;
  EndpointAddress _address;
  X509Certificate2 _certificate;
  
  public ServiceAccess() //Constructor
  {
      _binding = new BasicHttpsBinding(BasicHttpsSecurityMode.TransportWithCredential);
      //With Credential because I've read on many tutorials that if I don't use this mode, 
      //IIS will validate the certificate first and fail without ever calling my custom validator
  
      _certificate = new X509Certificate2(); //I don't know why 2 except for the identity code below
      Byte[] bytes = Loader.EmbeddedResources.ToByteArray("Namespace.Project.certificate.crt");
      _certificate.Import(bytes);
  
      EndpointIdentity identity = EndpointIdentity.Create509CertificateIdentity(_certificate);
  
      _address = new EndpointAddress(new Uri("https://www.service.com/secure"), identity);
  }
  
  public void Start()
  {
      ServiceClient client = new ServiceClient(_binding, _address);
      client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.Custom;
      client.ClientCredentials.ServiceCertificate.Authentication.CustomCertificateValidator = new CustomCertificateValidator();
  
      client.ClientCredentials.ClientCertificate.Certificate = _certificate; //why do I need to set this certificate again if I already defined it in the endpointAddress?
  
      try
      {
          client.DoSomething();
      }
      catch (Exception ex)
      {
          //Error estabilishing a secure connection
          //Error the certificate doesn't have a private key
      }
  }
  </code></pre>
  
  <p>And my Custom Validator:</p>
  
  <pre><code>public class CustomValidator : X509CertificateValidator
  {
      public override void Validate(X509Certificate2 certificate)
      {
          //This never gets called
          throw new Exception("Custom Validator Called!");
      }
  }
  </code></pre>
  
  <p>Since the certificate was issued by themselves, I'd like to add the custom validator to debug and make it skip whatever's causing the validation errors, but keep everything else as is. 
  How can I make this work?</p>