【问题标题】:How can I read the Active Directory schema programmatically如何以编程方式读取 Active Directory 架构
【发布时间】:2010-07-20 14:02:01
【问题描述】:

我做了一些编程来从 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]);
    }
}

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

有什么想法吗?

【问题讨论】:

    标签: active-directory ldap


    【解决方案1】:

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

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

    这应该 - 如果您拥有必要的权限 - 让您可以访问架构中定义的属性 - 例如 MandatoryPropertiesOptionalProperties

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

    这对你开始有帮助吗?

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


    (来源:mvps.org

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

    【讨论】:

    • 你的答案和 LDAP 浏览器都很棒。
    • @marc_s 能否请您指导我DirectoryEntry schema = deMyself.SchemaEntry; 这一行中deMyself 的类型是什么?
    • @prog1011:这是一个错字 - 应该是 entry 类型为 DirectoryEntry
    猜你喜欢
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多