title: WCF Error "HTTP request was forbidden with client authentication scheme 'Anonymous'"
body: <p>Edit: I kinda forgot about this question! I ended up remapping the configuration, the error disappeared and I couldn't find the reason.</p>
  
  <p>I have a WCF service that is hosted on IIS, and a Windows Forms client that is trying to connect to it.</p>
  
  <p>The WCF service should be secured, so the IIS web app is configured to use a certificate that is issued by the domain (the CA is on the domain) issued to "localhost" for our dev environment.</p>
  
  <p>The SSL settings on IIS is set to "Require SSL" and "Client certificates = Require".</p>
  
  <p>The WCF service uses custom binding, and the configuration in the web.config is as follows:</p>
  
  <p>
        
        
          
          
            
          
        </p>
  
  <pre><code>  &lt;diagnostics&gt;
      &lt;messageLogging logEntireMessage="true" logMalformedMessages="true"
        logMessagesAtTransportLevel="true" maxMessagesToLog="25" /&gt;
    &lt;/diagnostics&gt;
    &lt;services&gt;
      &lt;service behaviorConfiguration="SpecificBehaviorType" name="MyCom.Service"&gt;
        &lt;endpoint address="" binding="customBinding" bindingConfiguration="CustomBindingConfiguration" contract="MyCom.IMyService"&gt;
          &lt;!--&lt;identity&gt;
            &lt;dns value="localhost" /&gt;
          &lt;/identity&gt;--&gt;
        &lt;/endpoint&gt;
        &lt;!--&lt;host&gt;
          &lt;baseAddresses&gt;
            &lt;add baseAddress="http://localhost:8999/" /&gt;
          &lt;/baseAddresses&gt;
        &lt;/host&gt;--&gt;
      &lt;/service&gt;
    &lt;/services&gt;
    &lt;bindings&gt;
      &lt;customBinding&gt;
        &lt;binding name="CustomBindingConfiguration"&gt;
          &lt;MyComMessageEncoding innerMessageEncoding="textMessageEncoding" /&gt;
  
          &lt;httpsTransport maxReceivedMessageSize="10000000" maxBufferPoolSize="10000000" requireClientCertificate="true" /&gt;
  
  
        &lt;/binding&gt;
      &lt;/customBinding&gt;
  
    &lt;/bindings&gt;
    &lt;behaviors&gt;
      &lt;serviceBehaviors&gt;
        &lt;behavior name="SpecificBehaviorType"&gt;
          &lt;!-- To avoid disclosing metadata information, 
        set the value below to false and remove the metadata endpoint above before deployment --&gt;
          &lt;serviceMetadata httpGetEnabled="True"/&gt;
          &lt;!-- To receive exception details in faults for debugging purposes, 
        set the value below to true.  Set to false before deployment 
        to avoid disclosing exception information --&gt;
          &lt;serviceDebug includeExceptionDetailInFaults="True" /&gt;
        &lt;/behavior&gt;
      &lt;/serviceBehaviors&gt;
    &lt;/behaviors&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>To enable the client to connect to the service, we exported the "localhost" certificate as "localhost.cer" file and used that file in our code (this should be another trusted certificate specific for the client, but we are using the localhost one for testing purposes). 
  The client configuration is done programmatically (no specific reason why here according to my knowledge).</p>
  
  <p>The client code is as following:</p>
  
  <pre><code> Dim isHttps As Boolean = uri.ToLower().StartsWith("https")
          'TODO: set quotas for message size
          Dim binding As New CustomBinding()
          binding.Elements.Add(New MyComEncodingBindingElement(New TextMessageEncodingBindingElement(MessageVersion.Soap12, System.Text.Encoding.UTF8)))
          'TODO: make configurable to support both http and https
          If isHttps Then
              Dim httpsBinding As New HttpsTransportBindingElement
  
              binding.Elements.Add(httpsBinding)
          Else
              Dim transport As New HttpTransportBindingElement()
              binding.Elements.Add(transport)
          End If
  
          Dim channelFactory = New ChannelFactory(Of IRequestChannel)(binding, New EndpointAddress(uri))
  
          channelFactory.Endpoint.Behaviors.Add(New MustUnderstandBehavior(False))
  
          If isHttps Then
              For counter As Integer = 0 To channelFactory.Endpoint.Behaviors.Count - 1
                  If TypeOf channelFactory.Endpoint.Behaviors(counter) Is System.ServiceModel.Description.ClientCredentials Then
                      CType(channelFactory.Endpoint.Behaviors(counter), System.ServiceModel.Description.ClientCredentials).ClientCertificate.Certificate = New X509Certificate2("localhost.cer")
                      Exit For
                  End If
              Next
  
          End If
  
          channelFactory.Open()
          Dim channel = channelFactory.CreateChannel()
          channel.Open()
  </code></pre>
  
  <p>Now the problem is once we try to establish a connection, we get "HTTP request was forbidden with client authentication scheme 'Anonymous'"</p>
  
  <p>We tried to change the:</p>
  
  <p>httpsBinding.AuthenticationScheme</p>
  
  <p>but no use.</p>
  
  <p>What am I missing?</p>