title: WCF Custom Username and Password Validator using a Repository Database c#
body: <p>I have a WCF service that is configured programmatically - No XML config file.
  I am trying to implement a Custom Username Password Validation using a repository database table to verify against. I do not wish to require Windows Usernames / passwords on client - server. I am using NetTCPBinding.</p>
  
  <p>I know how to create a CustomValidator, and how to assign it to the ServiceBehavior. 
  My issue is that I do not know how to set the RepositoryConnection string for the validator when the service instantiates it (I am not sure how that mechanism works anyway.) 
  this property of my validator is what I need to set - programmatically - Please remember I am not using any XML CONFIG FILE .
  <strong>public string RepositoryConnection { get; set;}</strong></p>
  
  <p>I know my NetTCPBinding  will need to be changed and as this is my first WCF Security Attempt any comments are welcome and I will try to update question to provide any further info requested. I post relevant parts of code below. </p>
  
  <pre><code>public static NetTcpBinding SetNetTCPBinding(string name, string ns)
  {
      NetTcpBinding binding = new NetTcpBinding(); //SecurityMode.None);
      binding.Security.Mode = SecurityMode.Transport;
      binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
      binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
      binding.Security.Transport.ProtectionLevel = ProtectionLevel.None;
  }
  
  
  public class ServiceUserNamePasswordValidator : UserNamePasswordValidator
  {
      **public string RepositoryConnection { get; set;}**
  
      public override void Validate(string userName, string password)
      {
              try
              {
                  if (string.IsNullOrEmpty(userName) || password == null)
                  {
                      //throw new ArgumentNullException();
                      throw new FaultException("Username cannot be null or empty; Password cannot be null and should not be empty");
                  }
                  IGenericDataRepository&lt;MyAppDBData.Users&gt; repositorySingle = new GenericDataRepository&lt;MyAppDBData.Users&gt;(RepositoryConnection);
                  MyAppDBData.Users USER = new MyAppDBData.Users();
  
                  USER = repositorySingle.GetSingle(d =&gt; d.Name.ToLower() == userName.ToLower() &amp;&amp; d.Password == password);
  
                  if (USER == null)
                  {
                      // This throws an informative fault to the client.
                      throw new FaultException("Unknown Username or Incorrect Password");
                  }
              }
              finally{
              }
     }
  }
  </code></pre>
  
  <p>Here is where I have looked for answers - aside from gargling it.</p>
  
  <p><a href="https://stackoverflow.com/questions/7007411/wcf-authentication-custom-username-and-password-validator-asp-net">WCF Authentication: Custom Username and Password Validator asp.net</a></p>
  
  <p><a href="https://stackoverflow.com/questions/25325783/wcf-custom-username-and-password-validation-is-not-executed">WCF custom username and password validation is not executed</a></p>
  
  <p><a href="https://stackoverflow.com/questions/5183879/wcf-nettcpbinding?rq=1">WCF netTCPBinding</a></p>
  
  <p><a href="https://stackoverflow.com/questions/1544830/wcf-transportcredentialonly-not-sending-username-and-password">WCF TransportCredentialOnly not sending username and password</a></p>
  
  <p><a href="https://stackoverflow.com/questions/1923084/windows-authentication-encryption-in-wcf-with-nettcpbinding?rq=1">Windows Authentication / Encryption in WCF With NetTcpBinding</a></p>