title: Using C# to authenticate user against LDAP
body: <p>I'm using DirectorySearcher to search for a user entry in LDAP server.  </p>
  
  <pre><code>DirectoryEntry de = new DirectoryEntry();
  de.Path = "LDAP://myserver/OU=People,O=mycompany";
  de.AuthenticationType = AuthenticationTypes.None;
  
  DirectorySearcher deSearch = new DirectorySearcher();
  
  deSearch.SearchRoot = de;
  deSearch.Filter = "(uid=" + model.UserName + ")";
  
  SearchResult result = deSearch.FindOne();
  </code></pre>
  
  <p>I'm able to get th intended output in result variable.<br>
  However If I try to authenticate the same user by providing password in directory entry, I always get following error.  </p>
  
  <p>"The user name or password is incorrect."  </p>
  
  <pre><code>DirectoryEntry entry = new DirectoryEntry("LDAP://myserver/OU=People,O=mycompany", username, password);
  DirectorySearcher search = new DirectorySearcher(
      entry,
      "(uid=" + username + ")",
      new string[] { "uid" }
  );
  
  search.SearchScope = System.DirectoryServices.SearchScope.Subtree;
  SearchResult found = search.FindOne();   -&gt;&gt;&gt;&gt;&gt;this is where I get wrong credential error.
  </code></pre>
  
  <p>The username and password are for the user I want to authenticate.  </p>
  
  <p>Can anyone tell me what I'm doing wrong here or how to debug this.</p>