我想实现一个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了,但它不会是他们的方式来实现我想要的。
我知道我需要一些尝试渔获物和其他位代码作为然而,这是一个粗略的草稿。
感谢!!!!!!!!! – Sak 2017-04-17 08:57:09