Active Directory显示表中的所有属性 [英] Active Directory Display all properties in a table
问题描述
我试图实现一个LDAP查询,以收集我们拥有的有关用户的所有属性,而无需事先指定属性,我想在表中显示此内容,因此使用以下代码。如果我取消注释search.PropertiesToLoad.Add( cn);,则此方法有效。行,并显示我以相同方式添加的任何其他属性,但是当我对所有属性进行完全搜索时则不会。
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.
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的情况下循环所有属性,但不是它们的a
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.
我知道到目前为止,我还需要一些尝试捕获和代码中的其他内容,这是一个粗略的草稿。
I know I need a few try catches and other bits in the code as of yet, it's a rough draft.
推荐答案
可以使用 DirectoryEntry
完成此操作,但我认为 SearchResultCollection
具有所有字段。
尝试为每个搜索结果创建一个 DirectoryEntry
,它应该具有所有活动目录属性:
This can be done using DirectoryEntry
, but I don't think the SearchResultCollection
has all fields.
Try to create a DirectoryEntry
for every search result, it should have all active directory properties:
DirectoryEntry entry = result.GetDirectoryEntry();
另外,请注意,在活动目录中,每个属性都可以具有多个值(例如MemberOf字段),因此,您也必须对其进行迭代。
我写了类似的方法,但是我选择了一个带有键/值的 List
(它似乎比WCF更易于管理。 ILookup
是最佳选择,但我无法在这里使用它)。在这里,从try / catch / using中删除
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 List
with keys/values (it seemed more manageable over WCF. ILookup
would be optimal, but I couldn't get it to work here). Here it is, stripped from 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));
}
这篇关于Active Directory显示表中的所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!