【问题标题】:Active Directory : PropertiesToLoad get all propertiesActive Directory : PropertiesToLoad 获取所有属性
【发布时间】:2015-01-29 12:26:26
【问题描述】:

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

【问题讨论】:

标签: c# active-directory


【解决方案1】:

如果你没有在PropertiesToLoad 中指定任何东西,你应该得到所有的属性。只需删除带有search.PropertiesToLoad.Add 的行。

但是,获取域中所有用户的所有属性可能相当繁重。

【讨论】:

  • omg 我用 PropertiesToLoad.Add(String.Empty) 试过了,但没想到会这么简单。谢谢
  • 这太迟了,但请不要返回所有用户属性。正是这种结构糟糕的搜索对广告征税。考虑到 AD 将包含仅对 Exchange、Skype 等有意义的内部属性,返回 all 会带来一堆你永远不会使用的垃圾。
  • 有数百个属性,要加载默认不返回的属性,请查看链接docs.microsoft.com/en-us/windows/desktop/adschema/…并添加到加载列表。
  • @trix 如果您在这里查看drawed chapins 的答案:stackoverflow.com/questions/23176284/… 您会知道为什么要通过propertiestoLoad 进行限制。 VeryFast vs VerySlow 你打电话..
  • @Ken,我不知道您是不是想向其他人讲话,但我明确表示返回所有属性。帖子开头的评论建议 not 指定 PropertiesToLoad,我不同意这个建议。
【解决方案2】:

要获取所有属性名称,您可以这样做:

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

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

propertyNamesList 将包含那里的所有内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多