4

I have requirement to search for users in a LDAP directory using C#. I can do it using DirectoryEntry and DirectorySearcher as shown in code below:

SearchResultCollection sResults = null;

DirectoryEntry dEntry = new DirectoryEntry(_LDAPConnectionString);

DirectorySearcher dSearcher = new DirectorySearcher(dEntry);    
dSearcher.Filter = String.Format("(&(objectClass=user)(cn={0}))", userName);

sResults = dSearcher.FindAll();

But the requirement is to create a LdapConnection object using a standard access user (always the same) as shown below. And use that particular LdapConnectionObject to search users using username.

LdapConnection ldapConnectionObject = new LdapConnection(
                new LdapDirectoryIdentifier(_hostName, _port),
                null,
                AuthType.Basic);
ldapConnectionObject.Bind(accessUserCredential);

How do I use the above ldapConnectionObject to search for users?

1

1 Answer 1

9

I found the answer searching using LdapConnection object. So we can use the SendRequest method of the LdapConnection class using the SearchRequest to get search response back. In below example, I have searched the user with uid userName and retreived its DN.

ldapConnection = new LdapConnection(
                new LdapDirectoryIdentifier(_hostName, _port),
                null,
                AuthType.Basic
                );

string searchFilter = String.Format("(&(objectClass=user)(uid={0}))", userName);

string userStore = "OU=WebsiteUsers,OU=InternalUsers";

SearchRequest searchRequest = new SearchRequest
                (userStore,
                 searchFilter,
                 System.DirectoryServices.Protocols.SearchScope.Subtree,
                 new string[] { "DistinguishedName" });

var response = (SearchResponse)ldapConnection.SendRequest(searchRequest);
string userDN = response.Entries[0].Attributes["DistinguishedName"][0].ToString();
Sign up to request clarification or add additional context in comments.

6 Comments

But parameter _userStore. What is that?
Also: My (limited) tour of LDAP has told me that the parameter DistinguishedName is a default that is always returned. What I saw was that it is a "noobie mistake" to include that in your search parameters. I appreciate your code, though! :)
@jp2code the _userStore is the distinguished name of the object at which to start the search from. We can narrow it down as much as we can. and yeah it was a noobie mistake :) . I removed that search parameter from my code. Thanks :)
@Żubrówka the example for _userStore is : "OU=WebsiteUsers,OU=InternalUsers". It is just to specify from where the search should be started from.
This code was very helpful. Only in my case I could get the DN as response.Entries[0].DistinguishedName. The Attributes were coming back empty.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.