C# Active Directory 在表格中显示所有属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1091115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:00:07  来源:igfitidea点击:

Active Directory Display all properties in a table

c#active-directory

提问by PeteT

I am trying to achieve an LDAP query to gather all properties we have about our users without specifying the properties before hand, I would like to display this in a table so used the below code. This works if I uncomment the search.PropertiesToLoad.Add("cn"); line and will display any other properties I add in the same way but not when I do a full search for all properties.

我正在尝试实现 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;

The problem seems to be with

问题似乎与

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

I expected this to loop all properties when no PropertiesToLoad had been specified but it doesn't is their a way to achieve what I want to.

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

I know I need a few try catches and other bits in the code as of yet, it's a rough draft.

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

采纳答案by Kobi

This can be done using DirectoryEntry, but I don't think the SearchResultCollectionhas all fields.
Try to create a DirectoryEntryfor every search result, it should have all active directory properties:

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

DirectoryEntry entry = result.GetDirectoryEntry();

Also, note that in the active directory every property can have multiple values (like the MemberOf field), so you'll have to iterate them as well.
I've wrote a similar method, but I chose a Listwith keys/values (it seemed more manageable over WCF. ILookupwould be optimal, but I couldn't get it to work here). Here it is, stripped from try/catch/using

另请注意,在活动目录中,每个属性都可以有多个值(如 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));
   }

回答by Magnus Johansson

You can iterate through all properties this way:

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

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());
    }
  }
}

Is that what you need?

那是你需要的吗?

回答by New Bee

Came across this thread while looking on how to do this myself. Instead i found a different way of doing it, and seems to be working fine.

在自己寻找如何做到这一点时遇到了这个线程。相反,我找到了一种不同的方法,并且似乎工作正常。

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

Its loading perfectly find in a Combobox anyhow. Just incase anyone else comes across this thread.

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