Vaibhav Deshmukh

Vaibhav Deshmukh

  • 1.1k
  • 464
  • 12.2k

LDAP Authentication

Feb 27 2017 7:03 AM
I am badly stucked in implementing LDAP Authentication.
My moto is to find OU or(Groups) based on Username.Means find a particular group for user based on Username.I am trying below way but not getting what I expecting.Since, my credentials are valid it throws an exception "The user name or password is incorrect.":
Snap 1:
  1. DirectoryEntry entry = new DirectoryEntry("LDAP://IP Address:Port/CN=Users,DC=domain,DC=com",username,password);  
  2. DirectorySearcher ds = new DirectorySearcher(entry);  
  3. ds.Filter = "(&(objectClass=user)(cn=" + username + "))";  
  4. SearchResult results = ds.FindOne(); 
 Snap 2 :
The below code snippet works fine but it only confirms Authentication for me:
  1. using (LdapConnection ldap = new LdapConnection(ConfigurationManager.ConnectionStrings["ADConnectionString"].ConnectionString))  
  2.                {  
  3.                    ldap.AuthType = AuthType.Basic;  
  4.                    ldap.SessionOptions.ProtocolVersion = 3;  
  5.                    // Distinguished name is reuired to pass to the bind method  
  6.                    string distinguishedName = "cn=" + username + ",ou=Users,dc=domain,dc=com";  
  7.                    ldap.Bind(new NetworkCredential(distinguishedName, password));  
  8.                 } 
Please Correct Me ! As I am New to LDAP I don't know whats the replacement code for above circumstance.
1. Can I use any LDAP conection object or property so that I need not create any DirectoryEntry parameter (String Path,String Username,String password) ?
2. I have pair of two credentials set. One for Connection string to LDAP server and second one is for user. I tried both . But didn't find any way out.
3. How to create connection string for LDAP  or DirectoryEntry as connection parameter.?
Thanks In advance.

Answers (6)