How to get DirectoryEntry from DirectoryEntry and DN

I have a DirectoryEntry object representing a user. From the DirectoryEntry.Properties collection, I retrieve the "manager" property, which will give me the Distinguished Name ("DN") value for the user manager.

Is it possible to get a DirectoryEntry object for a manager from only these two objects? If so, how?

I am assuming something like DirectoryEntry.GetEntryFromDN(dnManager); but I can not find a similar call.

Just to clarify, DirectoryEntry and DN are the only pieces of information that I have. I cannot create a new DirectoryEntry , because then I would have to use the default directory and credentials or have the directory name / port, username and password.

+6
c # active-directory ldap
source share
2 answers
 DirectoryEntry User = YourPreExistingUser(); string managerDN = User.Properties["manager"][0].ToString(); // Browse up the object hierarchy using DirectoryEntry.Parent looking for the // domain root (domainDNS) object starting from the existing user. DirectoryEntry DomainRoot = User; do { DomainRoot = DomainRoot.Parent; } while (DomainRoot.SchemaClassName != "domainDNS"); // Use the domain root object we found as the search root for a DirectorySearcher // and search for the manager distinguished name. using (DirectorySearcher Search = new DirectorySearcher()) { Search.SearchRoot = DomainRoot; Search.Filter = "(&(distinguishedName=" + managerDN + "))"; SearchResult Result = Search.FindOne(); if (Result != null) { DirectoryEntry Manager = Result.GetDirectoryEntry(); } } 
+5
source share

You can create a new DirectoryEntry instance that provides the DN as an argument, and then try to bind (for example, by updating properties).

DirectoryEntry e = new DirectoryEntry (dn, "u", "p");
e.RefreshCache ();

0
source share

All Articles