title: Exception = The HTTP request is unauthorized with client authentication scheme 'Anonymous'
body: <p>Please see the app below:</p>
  
  <pre><code>public Form1()
          {
              try
              {
                  InitializeComponent();
                  ServiceReference1.Service1Client s1 = new ServiceReference1.Service1Client();
                  s1.ClientCredentials.UserName.UserName = "testuser";
                  s1.ClientCredentials.UserName.Password = "testpass";
                  string str = s1.GetData(1);
              }
              catch (System.ServiceModel.Security.MessageSecurityException e)
              {
                  //Handle an authentication failure
                  using (System.IO.StreamWriter file =
                     new System.IO.StreamWriter(@"C:\LogFile\log.txt"))
                  {
                      file.WriteLine("User Authentication Failed");
                  }
              }
  
          }
  </code></pre>
  
  <p>and the app.config below:</p>
  
  <pre><code>&lt;system.serviceModel&gt;
          &lt;bindings&gt;
              &lt;wsHttpBinding&gt;
                  &lt;binding name="WSHttpBinding_IService1"&gt;
                      &lt;security mode="TransportWithMessageCredential"&gt;
                          &lt;message clientCredentialType="UserName" /&gt;
                      &lt;/security&gt;
                  &lt;/binding&gt;
              &lt;/wsHttpBinding&gt;
          &lt;/bindings&gt;
          &lt;client&gt;
              &lt;endpoint address="https://hq-wk-is/WCFSSL/Service1.svc" binding="wsHttpBinding"
                  bindingConfiguration="WSHttpBinding_IService1" contract="ServiceReference1.IService1"
                  name="WSHttpBinding_IService1" /&gt;
          &lt;/client&gt;
      &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>Here is the web.config of the service:</p>
  
  <pre><code> &lt;system.serviceModel&gt;
      &lt;services&gt;
        &lt;service name="WcfService1.Service1"&gt;
          &lt;endpoint contract="WcfService1.IService1" binding="wsHttpBinding" bindingConfiguration="wsHttpBindingConfiguration"/&gt;
        &lt;/service&gt;
      &lt;/services&gt;
  
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;      
  
          &lt;!--added--&gt;
          &lt;behavior&gt;
            &lt;serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="false"/&gt;
            &lt;serviceCredentials&gt;
              &lt;userNameAuthentication  userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WcfService1.CustomUserNameValidator, WcfService1" /&gt;
            &lt;/serviceCredentials&gt;
          &lt;/behavior&gt;
  
          &lt;!--&lt;behavior&gt;
            &lt;serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="false"/&gt;
          &lt;/behavior&gt;--&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;protocolMapping&gt;
          &lt;add binding="basicHttpsBinding" scheme="https" /&gt;
      &lt;/protocolMapping&gt;
  
      &lt;!--added--&gt;
      &lt;bindings&gt;
        &lt;wsHttpBinding&gt;
          &lt;binding name="wsHttpBindingConfiguration" &gt;
            &lt;security mode="TransportWithMessageCredential" &gt;
              &lt;message clientCredentialType="UserName" /&gt;
            &lt;/security&gt;
          &lt;/binding&gt;
        &lt;/wsHttpBinding&gt;
      &lt;/bindings&gt;
  
      &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /&gt;
    &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>and the CustomValidator:</p>
  
  <pre><code>namespace WcfService1
  {
      public class CustomUserNameValidator : UserNamePasswordValidator
      {
          public override void Validate(string userName, string password)
          {
  
              using (System.IO.StreamWriter file =
              new System.IO.StreamWriter(@"C:\log\log.txt"))
              {
                  file.WriteLine("Hola");
              }
  
  
  
              if (null == userName || null == password)
              {
                  throw new ArgumentNullException();
              }
  
              if (!(userName == "testuser" &amp;&amp; password == "testpass"))
              {
                  // This throws an informative fault to the client.
                  throw new FaultException("Unknown Username or Incorrect Password");
                  // When you do not want to throw an infomative fault to the client,
                  // throw the following exception.
                  // throw new SecurityTokenException("Unknown Username or Incorrect Password");
              }
          }
      }
  }
  </code></pre>
  
  <p>I have enabled Basic Authentication only for this web application in IIS.</p>
  
  <p>The error I get is: <code>The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Basic realm="MyPC"'</code></p>
  
  <p>What is the problem? Why are the credentials being sent anonymously? I believe the credentials should be passed in the SOAP header in this case.</p>