【问题标题】:Active Directory Display all properties in a tableActive Directory 在表格中显示所有属性
【发布时间】:2009-07-07 08:53:16
【问题描述】:

我正在尝试实现一个 LDAP 查询来收集我们拥有的关于用户的所有属性,而无需事先指定属性,我想将其显示在一个表中,因此使用了下面的代码。如果我取消注释 search.PropertiesToLoad.Add("cn"); 这将有效行,并会显示我以相同方式添加的任何其他属性,但在我完全搜索所有属性时不会显示。

DirectoryEntry myLdapConnection = createDirectoryEntry();
DirectorySearcher search = new DirectorySearcher(myLdapConnection);

search.CacheResults = true;
//search.PropertiesToLoad.Add("cn");

SearchResultCollection allResults = search.FindAll();
DataTable resultsTable = new DataTable("Results");

//add columns for each property in results
foreach (string colName in allResults.PropertiesLoaded)
    resultsTable.Columns.Add(colName, colName.GetType());

//loop to add records to DataTable
foreach (SearchResult result in allResults)
{
    int tmp = result.Properties.Count;
    DataRow row = resultsTable.NewRow();
    foreach (string columnName in search.PropertiesToLoad)
    {
        if (columnName.Equals("lastlogon"))
        {
            if (result.Properties.Contains(columnName))
                row[columnName] = ConvertDate(result.Properties[columnName].ToString());
            else
                row[columnName] = "";
        }
        else
        {
            if (result.Properties.Contains(columnName))
                row[columnName] = result.Properties[columnName][0].ToString();
            else
                row[columnName] = "";
        }
    }
    resultsTable.Rows.Add(row);
}

gridResults.DataSource = resultsTable;

问题似乎出在

foreach (string colName in allResults.PropertiesLoaded)
    resultsTable.Columns.Add(colName, colName.GetType());

当没有指定 PropertiesToLoad 时,我希望这会循环所有属性,但这不是他们实现我想要的方式。

我知道我需要一些尝试捕获和代码中的其他部分,这是一个粗略的草案。

【问题讨论】:

    标签: c# active-directory


    【解决方案1】:

    这可以使用DirectoryEntry 完成,但我不认为SearchResultCollection 包含所有字段。
    尝试为每个搜索结果创建一个DirectoryEntry,它应该具有所有活动目录属性:

    DirectoryEntry entry = result.GetDirectoryEntry();
    

    另外,请注意,在活动目录中,每个属性都可以有多个值(例如 MemberOf 字段),因此您还必须对它们进行迭代。
    我写了一个类似的方法,但我选择了带有键/值的List(它似乎比 WCF 更易于管理。ILookup 将是最佳的,但我无法让它在这里工作)。在这里,从 try/catch/using 中剥离

    var list = new List<KeyValuePair<string, string>>();
    foreach (PropertyValueCollection property in entry.Properties)
       foreach (object o in property)
       {
           string value = o.ToString();
           list.Add(new KeyValuePair<string, string>(property.PropertyName, value));
       }
    

    【讨论】:

      【解决方案2】:

      您可以通过这种方式遍历所有属性:

      foreach (SearchResult searchResult in allResults)
      {
        foreach (string propName in searchResult.Properties.PropertyNames)
        {
          ResultPropertyValueCollection valueCollection =
          searchResult.Properties[propName];
          foreach (Object propertyValue in valueCollection)
          {
          Console.WriteLine("Property: " + propName + ": " + propertyValue.ToString());
          }
        }
      }
      

      这是你需要的吗?

      【讨论】:

        【解决方案3】:

        在自己研究如何执行此操作时遇到此线程。 相反,我找到了一种不同的方法,并且似乎工作正常。

        return ((DirectoryEntry)UserPrincipal.Current.GetUnderlyingObject()).Properties.PropertyNames;
        

        无论如何,它的加载完全可以在组合框中找到。 以防其他人遇到此线程。

        【讨论】:

        • 如果您的评论将 UserPrincipal 与 DirectorySearcher 或相关内容相关联,这可能对我更有帮助。我可能只是对这个对象不够了解,但我没有看到有效的对象类型,我无法解决这个问题。
        • 这是使用较新的 System.DirectoryServices.AccountManagement。所以你需要添加一个引用和使用语句。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多