1

我正在尝试使用 读取 Active Directory 架构数据DirectorySearcher,如下所示:

DirectoryEntry entry = new DirectoryEntry("LDAP://********/CN=Schema,*****");

var filter = "(&(objectCategory=attributeSchema))";
var directorySearcher = new DirectorySearcher(entry, filter);

var searchResult = directorySearcher.FindAll();
DirectoryEntry schemaTest2 = entry.SchemaEntry;

// error on this line: 
// "The directory cannot report the number of properties."
foreach (var prop in schemaTest2.Properties.PropertyNames) 
{
    string propName = prop.ToString();                    
}

有谁知道为什么会发生此错误?或者您对如何使用阅读 AD 架构有什么建议DirectorySerarcher

注意:我不能使用

ActiveDirectorySchema schema = ActiveDirectorySchema.GetCurrentSchema();

因为我面临着其他一些问题。

任何建议将不胜感激!

谢谢。

4

1 回答 1

1

我在这里遇到了同样的错误。微软的解决方案如下:

DirectoryEntry  myDirectoryEntry=new DirectoryEntry();
// Display the 'SchemaClassName'.
Console.WriteLine("Schema class name:" + myDirectoryEntry.SchemaClassName);

// Gets the SchemaEntry of the ADS object.
DirectoryEntry mySchemaEntry = myDirectoryEntry.SchemaEntry;

if (string.Compare(mySchemaEntry.Name, "container") == 0)
{
   foreach(DirectoryEntry myChildDirectoryEntry in myDirectoryEntry.Children)
      Console.WriteLine(myChildDirectoryEntry.Path);
}

在我的情况下,容器不起作用myDirectoryEntry.SchemaClassName = domainDNS,为了获得一些结果,我需要将代码修改为:

DirectoryEntry myDirectoryEntry = new DirectoryEntry();

    // Display the 'SchemaClassName'.
    Console.WriteLine("Schema class name:" + myDirectoryEntry.SchemaClassName);

    // Gets the SchemaEntry of the ADS object.
    DirectoryEntry mySchemaEntry = myDirectoryEntry.SchemaEntry;

    if (string.Compare(mySchemaEntry.Name, "domainDNS") == 0)
    {
        foreach (DirectoryEntry myChildDirectoryEntry in myDirectoryEntry.Children)
            Console.WriteLine(myChildDirectoryEntry.Path);
    }
    Console.ReadLine();

我希望它对你有帮助。

于 2018-08-31T11:44:39.920 回答