0

我正在尝试获取森林中的所有目录条目。

我的代码片段如下所示:

DirectoryContext dc = new DirectoryContext(DirectoryContextType.DirectoryServer, "xx.x.xxx.40", "w28\\administrator", "pwd");

Forest forest = Forest.GetForest(dc);

Console.WriteLine("Domain count in forest: " + forest.Domains.Count);
DomainCollection domains = forest.Domains;

foreach (Domain d in domains)
{
    Console.WriteLine(d.Name);

    //It doesn't work
    DirectoryEntry entry = d.GetDirectoryEntry();
    foreach (DirectoryEntry child in entry.Children)
    {
        Console.WriteLine(" - " + child.Name);
    }
}

但是,我得到一个例外:

System.DirectoryServices.ActiveDirectory.ActiveDirectoryOperationException: Unknown error (0x80005000) ---> System.Runtime.InteropServices.COMException (0x80005000): Unknown error (0x80005000)
   at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   at System.DirectoryServices.DirectoryEntry.Bind()
   at System.DirectoryServices.DirectoryEntry.get_AdsObject()
   at System.DirectoryServices.PropertyValueCollection.PopulateList()
   at System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName)
   at System.DirectoryServices.PropertyCollection.get_Item(String propertyName)
   at System.DirectoryServices.ActiveDirectory.PropertyManager.GetPropertyValue(DirectoryContext context, DirectoryEntry directoryEntry, String propertyName)
   --- End of inner exception stack trace ---
   at System.DirectoryServices.ActiveDirectory.PropertyManager.GetPropertyValue(DirectoryContext context, DirectoryEntry directoryEntry, String propertyName)
   at System.DirectoryServices.ActiveDirectory.DirectoryEntryManager.ExpandWellKnownDN(WellKnownDN dn)
   at System.DirectoryServices.ActiveDirectory.Domain.GetDirectoryEntry()
4

1 回答 1

1

如果我的几个域控制器被关闭并变得不可用,我也会遇到同样的错误。

我改变了获取域的DirectoryEntry实例的方式:

entry = new DirectoryEntry(string.Format("LDAP://{0}", d.Name), UserNameFull, password);

这可以正常工作,因为引发了“服务器无法运行”错误,因此它可以向用户指出其 DC 有问题。

于 2014-07-28T09:48:42.087 回答