title: Calling WCF Service with basic auth and client certificate
body: <p>We are writing a client to a WCF service that uses both a CSR certificate and basic authentication.</p>
  
  <p>Our C# client is generated via Visual Studio and we can programmatically set the certificate and the username/password. However, we have to manually send the Basic Auth header otherwise we receive the error:</p>
  
  <pre><code>'The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Basic realm="HttpBasicAuthentication"'.'
  </code></pre>
  
  <p>Our code is:</p>
  
  <pre class="lang-cs prettyprint-override"><code>var myBinding = new WSHttpBinding();
  myBinding.Security.Mode = SecurityMode.Transport;
  myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
  myBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
  
  var ea = new EndpointAddress("https://example.org/myservice");
  var client = new MandateWebServiceClient(myBinding, ea);
  client.ClientCredentials.UserName.UserName = "wally";
  client.ClientCredentials.UserName.Password = "walliesWorld";
  client.ClientCredentials.ClientCertificate.Certificate = new X509Certificate2("C:\\some\\path\\to\\csr.pfx", "password");
  
  using (var scope = new OperationContextScope(client.InnerChannel))
  {
      var httpRequestProperty = new HttpRequestMessageProperty();
      httpRequestProperty.Headers[HttpRequestHeader.Authorization] =
          "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(client.ClientCredentials.UserName.UserName + ":" + client.ClientCredentials.UserName.Password));
      OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
  
      client.create();
  }
  </code></pre>
  
  <p>With the above code, we can successfully talk to the service. If we remove the lines in the <code>using</code> block, the authentication scheme changes to <code>Anonymous</code>, and we get the error above.</p>
  
  <p>The above arrangement seems a little hackey. We have tried all the <code>SecurityMode</code> settings possible and <code>SecurityMode.Transport</code> with <code>HttpClientCredentialType.Certificate</code> is the only combination that allows the certificate to be accepted. Setting or not setting <code>MessageCredentialType.UserName</code> appears to have no effect on the system.</p>
  
  <p>Is there any .Net Framework way of providing both the certificate and the basic authentication header rather than manually adding the header?</p>