【发布时间】: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