title: WCF Service TransportWithMessageCredentials Customer validator
body: <p>I have configured a new Azure hosted WCF service with a custom username validator. The validator class will validate username and passwords from an existing aspnetUsers table in an Azure database. </p>
  
  <p>I have configured the service using TransportWithMessageCredentials binding so the clients will be providing their usernames and passwords in clear text in the request.  </p>
  
  <p>My code will then look up the user and get their hashed password from the database and then use this to hash the password sent via the service. If they match then allow the request.</p>
  
  <p>to validate the password I am using this code.</p>
  
  <pre><code> public static bool checkPassword(string hashedPassword, string password)
      {
  
          byte[] buffer4;
          if (hashedPassword == null)
          {
              return false;
          }
          if (password == null)
          {
              throw new ArgumentNullException("password");
          }
          byte[] src = Convert.FromBase64String(hashedPassword);
          if ((src.Length != 0x31) || (src[0] != 0))
          {
              return false;
          }
          byte[] dst = new byte[0x10];
          Buffer.BlockCopy(src, 1, dst, 0, 0x10);
          byte[] buffer3 = new byte[0x20];
          Buffer.BlockCopy(src, 0x11, buffer3, 0, 0x20);
          using (Rfc2898DeriveBytes bytes = new Rfc2898DeriveBytes(password, dst, 0x3e8))
          {
              buffer4 = bytes.GetBytes(0x20);
          }
          return ByteArraysEqual(buffer3, buffer4);
      }
  </code></pre>
  
  <p>So my question really is, Is sending the username and passwords in this way secure enough? As everything is going over https I am assuming it is but would like some guidance as I am fairly new to security in general.</p>
  
  <p>The service will also be IP restricted.</p>
  
  <p>Here is my service model config.</p>
  
  <pre><code> &lt;system.serviceModel&gt;
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name="MyServiceBehaviour"&gt;
            &lt;serviceCredentials&gt;
              &lt;userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="MyValidatorClass,MyNameSpace" /&gt;
            &lt;/serviceCredentials&gt;
            &lt;!-- To avoid disclosing metadata information, set the values below to false before deployment --&gt;
            &lt;serviceMetadata httpGetEnabled="true" httpsGetEnabled="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="false" /&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;bindings&gt;
        &lt;basicHttpBinding&gt;
          &lt;binding name="HttpBinding" maxReceivedMessageSize="2097152" receiveTimeout="00:02:00" sendTimeout="00:02:00"&gt;
          &lt;/binding&gt;
          &lt;binding name="HttpsBinding" maxReceivedMessageSize="2097152" receiveTimeout="00:02:00" sendTimeout="00:02:00"&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="MyService" behaviorConfiguration="MyServiceBehaviour"&gt;
          &lt;endpoint address="" binding="basicHttpBinding" bindingConfiguration="HttpsBinding" contract="MyContract" /&gt;
          &lt;host&gt;
            &lt;baseAddresses&gt;
              &lt;add baseAddress="https://MyServiceInAzure.net/" /&gt;
            &lt;/baseAddresses&gt;
          &lt;/host&gt;
        &lt;/service&gt;
      &lt;/services&gt;
      &lt;protocolMapping&gt;
        &lt;add binding="basicHttpsBinding" scheme="https" /&gt;
      &lt;/protocolMapping&gt;
      &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>And here is what the clients will be using:</p>
  
  <pre><code>Client.Service call = new Client.ServiceClient();
          call.ClientCredentials.UserName.UserName = "MyUsername";
          call.ClientCredentials.UserName.Password = "MyPassword";
  
          var result = call.PostCall("Hello World");
  </code></pre>
  
  <p>Thanks </p>