代码之家  ›  专栏  ›  技术社区  ›  Edison Chuang

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

  •  5
  • Edison Chuang  · 技术社区  · 15 年前

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

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

    有什么想法吗?

    1 回复  |  直到 7 年前
        1
  •  7
  •   marc_s MisterSmith    7 年前

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

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

    如果您有必要的权限,这应该允许您访问模式中定义的属性,比如 MandatoryProperties OptionalProperties :

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

    这有助于你开始吗??

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

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

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