我想實現一個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