title: Turn on Basic Authentication for a simple WCF service?
body: <p>I have a very simple WCF webservice that a customer is hosting on their own IIS.</p>
  
  <p>The customer has their own client that they've been testing against it, in their testing environment, and everything was working fine, until they disabled anonymous authentication and enabled basic. Once they did, they started getting errors:</p>
  
  <pre><code>The authentication schemes configured on the host ('Basic') do not allow those configured on the binding 'BasicHttpBinding' ('Anonymous'). Please ensure that the SecurityMode is set to Transport or TransportCredentialOnly. 
  </code></pre>
  
  <p>So, what do I need to do to get basic authentication working?</p>
  
  <p>My webservice's current <code>web.config</code>:</p>
  
  <pre><code>&lt;configuration&gt;
      &lt;system.web&gt;
          &lt;compilation debug="true" targetFramework="4.5" /&gt;
          &lt;httpRuntime targetFramework="4.5" /&gt;
      &lt;/system.web&gt;
      &lt;system.serviceModel&gt;
          &lt;behaviors&gt;
              &lt;serviceBehaviors&gt;
                  &lt;behavior name=""&gt;
                      &lt;serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /&gt;
                      &lt;serviceDebug includeExceptionDetailInFaults="false" /&gt;
                  &lt;/behavior&gt;
              &lt;/serviceBehaviors&gt;
          &lt;/behaviors&gt;
          &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true"
              multipleSiteBindingsEnabled="true" /&gt;
      &lt;/system.serviceModel&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>I have my own test client, that I can run against my webservice. Its current <code>app.config</code>:</p>
  
  <pre><code>&lt;configuration&gt;
      &lt;startup&gt;
          &lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /&gt;
      &lt;/startup&gt;
      &lt;system.serviceModel&gt;
          &lt;bindings&gt;
              &lt;basicHttpBinding&gt;
                  &lt;binding name="BasicHttpBinding_IMyServiceSvc" /&gt;
              &lt;/basicHttpBinding&gt;
          &lt;/bindings&gt;
          &lt;client&gt;
              &lt;endpoint address="http://localhost/MyServiceSvc.svc"
                  binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMyServiceSvc"
                  contract="MyServiceSvc.IMyServiceSvc"
                  name="BasicHttpBinding_IMyServiceSvc" /&gt;
          &lt;/client&gt;
      &lt;/system.serviceModel&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>When I install the service on my local IIS, and run the client against it, everything works, with anonymous authentication enabled.</p>
  
  <p>What do I need to do to turn on basic authentication, in IIS, and to configure both client and server to use basic authentication?</p>
  
  <p>My current test client code:</p>
  
  <pre><code>public class BlackHillsCompletionSvcTest
  {
      static void Main(string[] args)
      {
          using (var client = new MyServiceSvcClient())
          {
              var data = new Data
              {
                  id = "1",
                  description = "test data"
              };
  
              try
              {
                  var result = client.receiveData(data);
              }
              catch (Exception ex)
              {
                  var msg = ex.Message;
              }
          }
      }
  }
  </code></pre>
  
  <p>==Attempts:==</p>
  
  <p>As per Pankaj Kapare's suggestion, I added this to the webservice's <code>web.config</code>:</p>
  
  <pre><code>&lt;system.serviceModel&gt;
      &lt;bindings&gt;
          &lt;basicHttpBinding&gt;
              &lt;binding name="httpBinding"&gt;
                  &lt;security mode="TransportCredentialOnly"&gt;
                      &lt;transport clientCredentialType="Basic" /&gt;
                  &lt;/security&gt;
              &lt;/binding&gt;
          &lt;/basicHttpBinding&gt;
      &lt;/bindings&gt;
      ...
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>With that, I got an error when creating the client:</p>
  
  <pre><code>The binding at system.serviceModel/bindings/basicHttpBinding does not have a configured binding named 'BasicHttpBinding_IMyServiceSvc'. This is an invalid value for bindingConfiguration. 
  </code></pre>
  
  <p>So I added this to the client's <code>app.config</code>:</p>
  
  <pre><code>&lt;system.serviceModel&gt;
      &lt;bindings&gt;
          &lt;basicHttpBinding&gt;
              &lt;binding name="BasicHttpBinding_IMyServiceSvc"&gt;
                  &lt;security mode="TransportCredentialOnly"&gt;
                      &lt;transport clientCredentialType="Basic" /&gt;
                  &lt;/security&gt;
              &lt;/binding&gt;
          &lt;/basicHttpBinding&gt;
      &lt;/bindings&gt;
      ...
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>With that, I got another error:</p>
  
  <pre><code>The username is not provided. Specify username in ClientCredentials.
  </code></pre>
  
  <p>So I added username and password in my client:</p>
  
  <pre><code>using (var client = new MyServiceSvcClient())
  {
      client.ClientCredentials.UserName.UserName = "myusername";
      client.ClientCredentials.UserName.Password = "mypassword";
      ...
  }
  </code></pre>
  
  <p>And with that, I get yet another error:</p>
  
  <pre><code>The requested service, 'http://localhost/MyServiceSvc.svc' could not be activated.  Which got me wondering. So I loaded the .svc page in my browser, was asked to log in, and after I did, I saw this:
  
  The authentication schemes configured on the host ('Basic') do not allow those configured on the binding 'BasicHttpBinding' ('Anonymous').  Please ensure that the SecurityMode is set to Transport or TransportCredentialOnly.  Additionally, this may be resolved by changing the authentication schemes for this application through the IIS management tool, through the ServiceHost.Authentication.AuthenticationSchemes property, in the application configuration file at the &lt;serviceAuthenticationManager&gt; element, by updating the ClientCredentialType property on the binding, or by adjusting the AuthenticationScheme property on the HttpTransportBindingElement.
  </code></pre>
  
  <p>Which is what the customer is seeing.</p>
  
  <p>Ideas?</p>
  
  <p>== Maybe a solution? ==</p>
  
  <p>I think what may have been missing was a service definition, on the server, tying the binding to the endpoint:</p>
  
  <pre><code>&lt;system.serviceModel&gt;
      &lt;bindings&gt;
          &lt;basicHttpBinding&gt;
              &lt;binding name="httpBinding"&gt;
                  &lt;security mode="TransportCredentialOnly"&gt;
                      &lt;transport clientCredentialType="Basic" /&gt;
                  &lt;/security&gt;
              &lt;/binding&gt;
          &lt;/basicHttpBinding&gt;
      &lt;/bindings&gt;
      &lt;services&gt;
          &lt;service
                  name="MyServiceSvcNs.MyServiceSvc"
                  behaviorConfiguration="ServiceWithMetaData"
                  &gt;
              &lt;endpoint name="Default"
                  address=""
                  binding="basicHttpBinding"
                  bindingConfiguration="httpBinding"
                  contract="MyServiceSvcNs.IMyServiceSvc"
                  /&gt;
          &lt;/service&gt;
      &lt;/services&gt;
      &lt;behaviors&gt;
          &lt;serviceBehaviors&gt;
              &lt;behavior name="ServiceWithMetaData"&gt;
                  &lt;serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /&gt;
                  &lt;serviceDebug includeExceptionDetailInFaults="false" /&gt;
              &lt;/behavior&gt;
          &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true"
          multipleSiteBindingsEnabled="true" /&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>With this, things seem to be working. At least at first glance.</p>