title: Client certificate authentication WCF
body: <p>So I'm completely lost with certificates. I've searched all over the web for solutions and tutorials for this and found nothing that can really help me.
  What I'm trying to do is to have both server and client certificate validation for my WCF client-server application. The application is hosted on IIS.
  I want it on my dev computer (the server is localhost) and in test (where im the client and the server is a windows server).</p>
  
  <p>the configuration I have now is:</p>
  
  <p>Client:</p>
  
  <pre><code>&lt;behaviors&gt;
    &lt;endpointBehaviors&gt;
      &lt;behavior name="myBehaviorConfig"&gt;
        &lt;clientCredentials&gt;
          &lt;clientCertificate findValue="CN=MyTestClientCertificate"
                              storeLocation="LocalMachine"
                              x509FindType="FindBySubjectDistinguishedName"/&gt;
        &lt;/clientCredentials&gt;
      &lt;/behavior&gt;
    &lt;/endpointBehaviors&gt;
  &lt;/behaviors&gt;
  
  &lt;bindings&gt;
    &lt;wsHttpBinding&gt;
      &lt;binding name="MyBindingConfig"&gt;
        &lt;security mode="TransportWithMessageCredential"&gt;
          &lt;transport realm=""/&gt;
          &lt;message clientCredentialType="Certificate"/&gt;
        &lt;/security&gt;
      &lt;/binding&gt;
    &lt;/wsHttpBinding&gt;
  &lt;/bindings&gt;
  
  &lt;client&gt;
    &lt;endpoint address="https://localhost/Service.Calculator.svc"
              binding="wsHttpBinding"
              bindingConfiguration="MyBindingConfig"
              behaviorConfiguration="MyBehaviorConfig"
              contract="Service.ICalculator"
              name="ICalculatorServiceEndpoint"&gt;
      &lt;identity&gt;
        &lt;servicePrincipalName value="host"/&gt;
      &lt;/identity&gt;
    &lt;/endpoint&gt;    
  &lt;/client&gt;
  </code></pre>
  
  <p>Server:</p>
  
  <pre><code> &lt;behaviors&gt;
    &lt;serviceBehaviors&gt;
      &lt;behavior name="myBehavior"&gt;
        &lt;serviceCredentials&gt;
          &lt;serviceCertificate findValue="CN=MyTestRootCA"
                              storeLocation="LocalMachine"
                              x509FindType="FindBySubjectDistinguishedName"/&gt;
          &lt;userNameAuthentication userNamePasswordValidationMode="Windows"/&gt;
          &lt;clientCertificate&gt;
            &lt;authentication certificateValidationMode="PeerOrChainTrust"/&gt;
          &lt;/clientCertificate&gt;
        &lt;/serviceCredentials&gt;
        &lt;serviceMetadata httpsGetEnabled="true"/&gt;
        &lt;serviceDebug includeExceptionDetailInFaults="true"/&gt;
        &lt;unity operationContextEnabled="true"
               instanceContextEnabled="true"
               contextChannelEnabled="true"
               serviceHostBaseEnabled="true"/&gt;
      &lt;/behavior&gt;
    &lt;/serviceBehaviors&gt;
  &lt;/behaviors&gt;
  
  &lt;bindings&gt;
    &lt;wsHttpBinding&gt;
      &lt;binding name="MyBinding"&gt;
        &lt;security mode="TransportWithMessageCredential"&gt;
          &lt;transport realm=""/&gt;
          &lt;message clientCredentialType="Certificate"/&gt;
        &lt;/security&gt;
      &lt;/binding&gt;
    &lt;/wsHttpBinding&gt;
  &lt;/bindings&gt;
  
  &lt;services&gt;
    &lt;service name="Service.Calculator"
             behaviorConfiguration="myBehavior"&gt;
      &lt;endpoint address=""
                binding="wsHttpBinding"
                bindingConfiguration="MyBinding"
                contract="Service.ICalculator" /&gt;
      &lt;endpoint address="mex"
                binding="mexHttpsBinding"
                contract="IMetadataExchange"
                name="CalculatorServiceMex" /&gt;
    &lt;/service&gt;
  &lt;/services&gt;
  </code></pre>
  
  <p>"CN=MyTestRootCA" is the "Authority" the "Creating" certificate and I put him in the trusted root certificates on the localComputer as well as in the personal directory in the local computer.
  And it is the issuer of the client certificate "CN=MyTestClientCertificate".</p>
  
  <p>Few things..</p>
  
  <p>I know that the client certificate should be in the CurretUser directory in the "MMC" but when its there i have an exception that the app can't find the certificate.
  I tried locating it by "FindBySubjectDistinguishedName" and with "FindByThumbprint", both time was the same exception "Cant find certificate with the given criteria ..." so i put it in the LocalMachine and its fine.
  Any one has an idea why it didn't work?</p>
  
  <p>I had lots of problems and exceptions with this =\ the current one is:
  "The private key is not presented in the X.509 certificate"
  Anybody familiar with this exception and know how to fix it?</p>
  
  <p>thanks a lot for your answers, i'm sitting on this for few days now</p>