简体   繁体   中英

How to retrieve DirectoryEntry from a DirectoryEntry and a DN

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

Can I retrieve a DirectoryEntry object for the manager from just these two objects? If so, how?

I'm envisioning something like DirectoryEntry.GetEntryFromDN(dnManager); , but I cannot find a similar call.

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

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's 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();
    }
}

You can create a new DirectoryEntry instance providing the the DN as argument and then attempt to bind (by refreshing properties for example).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM