Active Directory:PropertiesToLoad获取所有属性

时间:2015-01-29 12:26:27

标签: c# active-directory

我尝试从Active Directory中的Object获取所有属性的列表。

我现在拥有的是:

List<User> users = new List<User>();
try
{
    DirectoryEntry root = new DirectoryEntry("LDAP://RootDSE");
    root = new DirectoryEntry("LDAP://" + root.Properties["defaultNamingContext"][0]);
    DirectorySearcher search = new DirectorySearcher(root);
    search.Filter = "(&(objectClass=user)(objectCategory=person))";

    search.PropertiesToLoad.Add("samaccountname");
    search.PropertiesToLoad.Add("displayname");
    search.PropertiesToLoad.Add("mail");
    search.PropertiesToLoad.Add("telephoneNumber");
    search.PropertiesToLoad.Add("department");
    search.PropertiesToLoad.Add("title");

    SearchResultCollection results = search.FindAll();
    if (results != null)
    {
        foreach (SearchResult result in results)
        {
            foreach (DictionaryEntry property in result.Properties)
            {
                Debug.Write(property.Key + ": ");
                foreach (var val in (property.Value as ResultPropertyValueCollection)) { 
                    Debug.Write(val +"; ");
                }
                Debug.WriteLine("");
            }
        }
    }
}
catch (Exception ex)
{

}

但它只获得我使用PropertiesToLoad添加的属性。是否可以动态获取所有属性?

2 个答案:

答案 0 :(得分:9)

如果您未在PropertiesToLoad中指定任何内容,则应获取所有属性。只需删除search.PropertiesToLoad.Add

然而,获取域中所有用户的所有属性可能非常繁重。

答案 1 :(得分:0)

要获取所有propertyNames,您可以执行以下操作:

 SearchResult result = search.FindOne();
 ResultPropertyCollection temp = result.Properties;

 string[] propertyNamesList = new string[temp.PropertyNames.Count];
 temp.PropertyNames.CopyTo(propertyNamesList, 0);

和propertyNamesList将包含所有内容。