title: WCF basicHttpBinding with userName and password from a WPF app
body: <p>I have read some questions here in SO and some linked tutorial.</p>
  
  <p>I have done all the stuff, but I have a problem... the authentication doesn't work.. it gives an exception only if I send a random username, without regarding about the password.</p>
  
  <p>I think, maybe, because the validator I have written it's never called ( I have breakpointed it)... but why? </p>
  
  <p>This is my app.confing of the wcf:</p>
  
  <p>
    </p>
  
  <pre><code>&lt;appSettings&gt;
    &lt;add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /&gt;
    &lt;add key="userName" value="piero"/&gt;
    &lt;add key="password" value="piero"/&gt;
  &lt;/appSettings&gt;
  &lt;system.web&gt;
    &lt;compilation debug="true" /&gt;
  &lt;/system.web&gt;
  &lt;!-- Quando si distribuisce il progetto della libreria di servizi, è necessario aggiungere il contenuto del file di configurazione al file 
  app.config dell'host. System.Configuration non supporta i file di configurazione delle librerie. --&gt;
  &lt;system.serviceModel&gt;
    &lt;!--&lt;standardEndpoints/&gt;--&gt;
    &lt;bindings&gt;
      &lt;basicHttpBinding&gt;
        &lt;binding name="usernameHttps"&gt;
          &lt;security mode="TransportWithMessageCredential"&gt;
            &lt;message clientCredentialType="UserName"/&gt;
          &lt;/security&gt;
        &lt;/binding&gt;
      &lt;/basicHttpBinding&gt;
    &lt;/bindings&gt;
    &lt;services&gt;
      &lt;service name="calc.CalcService" behaviorConfiguration="customBehavior"&gt;
        &lt;endpoint address="" binding="basicHttpBinding" bindingConfiguration=""
          name="base" contract="calc.ICalcService"  &gt;
          &lt;identity&gt;
            &lt;dns value="localhost"  /&gt;
          &lt;/identity&gt;
        &lt;/endpoint&gt;
        &lt;endpoint address="mex" binding="mexHttpBinding" name="mex" contract="IMetadataExchange" /&gt;
        &lt;endpoint address="web" behaviorConfiguration="webHttp" binding="webHttpBinding"
          bindingConfiguration="" name="web" contract="calc.ICalcService"&gt;
          &lt;identity&gt;
            &lt;dns value="localhost" /&gt;
          &lt;/identity&gt;
        &lt;/endpoint&gt;
        &lt;host&gt;
          &lt;baseAddresses&gt;
            &lt;add baseAddress="http://localhost:8733/Design_Time_Addresses/calc/" /&gt;
          &lt;/baseAddresses&gt;
        &lt;/host&gt;
      &lt;/service&gt;
    &lt;/services&gt;
    &lt;behaviors&gt;
      &lt;endpointBehaviors&gt;
        &lt;behavior name="webHttp"&gt;
          &lt;webHttp/&gt;
        &lt;/behavior&gt;
      &lt;/endpointBehaviors&gt;
            &lt;serviceBehaviors&gt;
      &lt;behavior &gt;
  
        &lt;serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/&gt;
  
  
        &lt;serviceDebug includeExceptionDetailInFaults="False" /&gt;
      &lt;/behavior&gt;
      &lt;behavior name="customBehavior"&gt;
        &lt;serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/&gt;
        &lt;serviceDebug includeExceptionDetailInFaults="True" /&gt;
        &lt;serviceCredentials&gt;
          &lt;userNameAuthentication
            userNamePasswordValidationMode="Custom"
            customUserNamePasswordValidatorType="calc.CustomPass, calc"/&gt;
        &lt;/serviceCredentials&gt;
      &lt;/behavior&gt;
  
      &lt;/serviceBehaviors&gt;
    &lt;/behaviors&gt;
    &lt;protocolMapping&gt;
      &lt;add binding="webHttpBinding" scheme="http"  /&gt;
    &lt;/protocolMapping&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p></p>
  
  <p>and this is the config of the calling wpf app:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
    &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;!--&lt;binding name="base" /&gt;--&gt;
                  &lt;binding name="base"&gt;
                    &lt;security mode="TransportCredentialOnly"&gt;
                      &lt;transport clientCredentialType="Basic"/&gt;
                    &lt;/security&gt;
                  &lt;/binding&gt;
                &lt;/basicHttpBinding&gt;
            &lt;/bindings&gt;
            &lt;client&gt;
                &lt;endpoint address="http://localhost:8733/Design_Time_Addresses/calc/"
                    binding="basicHttpBinding" bindingConfiguration="base" contract="calcTest.ICalcService"
                    name="base" /&gt;
            &lt;/client&gt;
        &lt;/system.serviceModel&gt;
      &lt;appSettings&gt;
        &lt;add key="userName" value="aaa"/&gt;
        &lt;add key="password" value="bbb"/&gt;
      &lt;/appSettings&gt;
    &lt;/configuration&gt;
  </code></pre>
  
  <p>and this is the validator of the wcf:</p>
  
  <pre><code>public class CustomPass : UserNamePasswordValidator //System.IdentityModel.dll
  {
      private const string USERNAME_ELEMENT_NAME = "userName";
  
      private const string PASSWORD_ELEMENT_NAME = "password";
  
      private const string FAULT_EXCEPTION_MESSAGE = "UserName or Password is incorrect!";
  
      public override void Validate(string userName, string password)
      {
          //Guarder.Guard.ArgumentNotNull(userName).ArgumentNotNull(password);
          var validateUserName = ConfigurationManager.AppSettings[USERNAME_ELEMENT_NAME];
          var validatePassword = ConfigurationManager.AppSettings[PASSWORD_ELEMENT_NAME];
          var validateCondition = userName.Equals(validateUserName) &amp;&amp; password.Equals(validatePassword);
          if (!validateCondition)
          {
              throw new FaultException(FAULT_EXCEPTION_MESSAGE);
          }
      }
  }
  </code></pre>
  
  <p>So:</p>
  
  <ul>
  <li>the wcf service starts correctly;</li>
  <li><p>the wpf app calls it  (basicHttpBinding) and uses it but:</p>
  
  <ol>
  <li><p>if there is no username, it gets error from wcf (this is ok, I think)</p></li>
  <li><p>the wcf service accepts ALL username, not only the one I have set in the     validator</p></li>
  <li>the password is useless, I can send anything, but only the username seems     </li>
  </ol></li>
  </ul>
  
  <p>to be read in some way but the wcf</p>
  
  <p>How can I use my validator? Why it doesn't start?</p>
  
  <p>I call the wcf in this way (any errors here?):</p>
  
  <pre><code>private void Add(object sender, RoutedEventArgs e)
  {
      var n1 = this.opOne.Text.ToString();
      var n2 = this.opTwo.Text.ToString();
      var calc = new calcTest.CalcServiceClient();
      calc.ClientCredentials.UserName.UserName = "piero";
      calc.ClientCredentials.UserName.Password = "piero";
      this.res.Text = calc.Add(n1, n2);
  }
  </code></pre>