简体   繁体   English

Active Directory显示表中的所有属性

[英]Active Directory Display all properties in a table

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. 我试图实现一个LDAP查询来收集我们有关用户的所有属性,而不事先指定属性,我想在表中显示这个,所以使用下面的代码。 This works if I uncomment the search.PropertiesToLoad.Add("cn"); 如果我取消注释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. line并将以相同的方式显示我添加的任何其他属性,但在我对所有属性进行完整搜索时则不会。

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. 我知道我在代码中需要一些尝试捕获和其他位,但这是一个草稿。

This can be done using DirectoryEntry , but I don't think the SearchResultCollection has all fields. 这可以使用DirectoryEntry完成,但我不认为SearchResultCollection具有所有字段。
Try to create a DirectoryEntry for every search result, it should have all active directory properties: 尝试为每个搜索结果创建一个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. 另请注意,在活动目录中,每个属性都可以有多个值(如MemberOf字段),因此您也必须迭代它们。
I've wrote a similar method, but I chose a List with keys/values (it seemed more manageable over WCF. ILookup would be optimal, but I couldn't get it to work here). 我写了一个类似的方法,但是我选择了一个带有键/值的List (它似乎比WCF更易于管理ILookup将是最佳的,但我无法在这里工作)。 Here it is, stripped from try/catch/using 在这里,它是从try / catch /使用中删除的

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

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? 这就是你需要的吗?

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. 无论如何,它的装载完美地在Combobox中找到。 Just incase anyone else comes across this thread. 只是想让其他人遇到这个帖子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM