如何以编程方式读取Active Directory架构

Edi*_*ang 5 ldap active-directory

我做了一些编程来读取Active Directory中的数据,例如用户帐户或组织信息等.下面的代码就像我做的那样.

DirectoryEntry entry = new DirectoryEntry(
    "LDAP://CN=Users,DC=domain,DC=com",
    null,
    null,
    AuthenticationTypes.Secure
    );

DirectorySearcher search = new DirectorySearcher(entry);

using (SearchResultCollection src = search.FindAll())
{
    foreach (SearchResult result in src)
    {
        Console.WriteLine(result.Properties["name"][0] + " : " + 
                          result.Properties["department"][0]);
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是如何才能知道目标对象具有哪些属性,然后我可以使用它们在获取所有数据之前过滤数据.

有任何想法吗?

mar*_*c_s 7

如果你有DirectoryEntry,你可以检查它.SchemaEntry:

DirectoryEntry entry = new DirectoryEntry("LDAP://......");

DirectoryEntry schema = entry.SchemaEntry;
Run Code Online (Sandbox Code Playgroud)

这应该 - 如果您具有必要的权限 - 允许您访问模式中定义的属性 - 例如MandatoryPropertiesOptionalProperties:

foreach (var prop in schema.Properties.PropertyNames)
{
   string propName = prop.ToString();
   var propValue = schema.Properties[propName].Value;
}
Run Code Online (Sandbox Code Playgroud)

这有助于你入门吗?

您可能还想看看BeaverTail - 我的C#开源LDAP浏览器.

alt text http://adsi.mvps.org/adsi/CSharp/beavertail1.png

它允许您检查任何LDAP节点并查看其所有属性.