title: How to validate LDAP path against Active Directory
body: <p>How to validate <code>LDAP</code> path? I have three <code>textboxes</code>, where I am allowing user to enter LDAP Path, username and password. I am able to validate username and password, but when it comes to validating LDAP path, it works initially, but after sometime, it allows invalid path as well. </p>
  
  <p>Valid LDAP path:</p>
  
  <blockquote>
    <p>192.168.12.12:565</p>
  </blockquote>
  
  <p>Invalid LDAP path:</p>
  
  <blockquote>
    <p>gfg192.168.12.12:565fgfgf</p>
  </blockquote>
  
  <p>and the user is able to get list of users using invalid path.</p>
  
  <p>I have tried <code>LdapConnection</code>, using <code>Directory Entry</code> and using <code>PrincipalContext</code> :</p>
  
  <pre><code>LdapConnection connection = new LdapConnection(txtLDAPPath.Text.Trim());
  NetworkCredential credential = new NetworkCredential(txtADUserName.Text.Trim(), password);
  connection.Credential = credential;
  connection.Bind();
  
  using (DirectoryEntry entry = new DirectoryEntry())
  {
      entry.Username = txtADUserName.Text.Trim();
      entry.Password = password;
      entry.Path = txtLDAPPath.Text;
      DirectorySearcher search = new DirectorySearcher(entry);
      search.Filter = "(&amp;(objectClass=user)(objectCategory=person)(!userAccountControl:1.2.840.113556.1.4.803:=2))";
      object obj = entry.NativeObject;
      SearchResult resultCol = search.FindOne();
  }
  
  
  PrincipalContext ctx = new PrincipalContext(ContextType.Domain,"Domain");
  bool Validate= ctx.ValidateCredentials(txtADUserName.Text, password);
  </code></pre>