C# · 12月 23, 2021

c# – Active Directory:PropertiesToLoad获取所有属性

我尝试从Active Directory中的对象获取所有属性的列表.

我现在拥有的是:

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添加的属性.是否可以动态获取所有属性?

解决方法 如果未在PropertiesToLoad中指定任何内容,则应获取所有属性.只需使用search.PropertiesToLoad.Add删除这些行.

但是,获取域中所有用户的所有属性可能非常繁重.