C# 给定DirectoryEntry,如何获取林对象?

C# 给定DirectoryEntry,如何获取林对象?,c#,active-directory,C#,Active Directory,假设我想实现一个扩展方法,它允许我为每个DirectoryEntry对象获取林对象 我的基本代码: public static Forest GetForest(DirectoryEntry de) { string forestDnsName = //DoSomethingWith(de.Path); ? DirectoryContext context = new DirectoryContext(DirectoryContextType.Forest, forestDnsN

假设我想实现一个扩展方法,它允许我为每个DirectoryEntry对象获取林对象

我的基本代码:

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

但是我不确定我怎样才能到达森林。有人知道如何获取它吗?

没有示例代码,只是一些常规步骤:

  • 获取“DifferentiedName”属性
  • 从DN中获取“DC=xxx,DC=yyy”段
  • 将上面的内容转换为域名 (删除所有“DC=”,然后将“替换为”。)
  • 连接到LDAP://[域名]/RootDSE
  • 阅读“rootDomainNamingContext”
  • 通过重复步骤3转换为林名称
  • 您已经知道如何从林名称创建
    对象
  • 如果您将多次调用此扩展方法,您可以为
    Forest
    对象映射构建一个域名,以减少广告流量(跳过步骤4-7)