简体   繁体   中英

Given a DirectoryEntry, how can I get the Forest object?

Let's say I would like to implement an Extension method that allows me to get the Forest object for every DirectoryEntry object.

My skeleton code:

public static Forest GetForest(DirectoryEntry de) {
    string forestDnsName = //DoSomethingWith(de.Path); ?
    DirectoryContext context = new DirectoryContext(DirectoryContextType.Forest, forestDnsName);
    return Forest.GetForest(context)
}

But I am unsure how I could get the Forest. Does anyone know how to get it?

No sample code, just some general steps:

  1. Get the "distinguishedname" attribute.
  2. Get the "DC=xxx,DC=yyy" segment from the DN
  3. Convert above to domain name (remove all "DC=", then replace "," with ".")
  4. Connect to LDAP://[domain name]/RootDSE
  5. Read "rootDomainNamingContext"
  6. Convert to forest name by repeating step 3
  7. You know how to create Forest object from forest name already

If you will call this extension method many times, you may build a domain name to Forest object map to reduce AD traffic (skip step 4 - 7).

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