How to do a search of LDAP user data on Apache Directory Studio with System.DirectoryServices?

798 Views Asked by At

I connected on LDAP Server of Apache Directory Studio and binded user with his credentials but now I want to fetch his own data like mail, telephone number etc. How to do that in System.DirectoryServices? Here's what I did up to this point?

LdapDirectoryIdentifier id= new LdapDirectoryIdentifier("localhost", 10389);
                LdapConnection conn=
                    new LdapConnection(id);
                var username= text_field_for_username.Text;
                var pass= text_field_for_pass.Text;
                conn.AuthType = AuthType.Basic;
                conn.SessionOptions.ProtocolVersion = 3;
                NetworkCredential param= new NetworkCredential("uid="+username+",ou=employees,dc=company,dc=com",pass);
conn.Bind(param);

And it worked. Now, how to fetch data of authenticated user using System.DirectoryServices? By the way, I know about possibility of non-existing user and I will add try and catch block for that purpose.

1

There are 1 best solutions below

0
EricLavault On

Basically you can do the following, using DirectoryServices SearchRequest and SearchResponse:

// ...

// Just for convenience 
conn.Credential = new NetworkCredential(userDN, userPass);
conn.Bind();

// The attributes to read, use "*" to request all user attributes.
var attr = new[] { "uid", "displayName", "mail" };

// Set userDN as basedn and search scope to Base to search user's own entry (filter is null)
SearchRequest req = new SearchRequest(userDN, (string) null, SearchScope.Base, attr);
var response = (SearchResponse) conn.SendRequest(req);

var entry = response.Entries[0];
// ... 

See documentation : SearchRequest, SearchResponse