Pet*_*teT 6 c# active-directory
我试图实现一个LDAP查询来收集我们有关用户的所有属性,而不事先指定属性,我想在表中显示这个,所以使用下面的代码.如果我取消注释search.PropertiesToLoad.Add("cn"); 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;
Run Code Online (Sandbox Code Playgroud)
问题似乎与
foreach (string colName in allResults.PropertiesLoaded)
resultsTable.Columns.Add(colName, colName.GetType());
Run Code Online (Sandbox Code Playgroud)
我预计这会在没有指定PropertiesToLoad的情况下循环所有属性,但它不是实现我想要的方式.
我知道我在代码中需要一些尝试捕获和其他位,但这是一个草稿.
Kob*_*obi 12
这可以使用DirectoryEntry,但我认为没有SearchResultCollection所有字段.
尝试DirectoryEntry为每个搜索结果创建一个,它应该具有所有活动目录属性:
DirectoryEntry entry = result.GetDirectoryEntry();
Run Code Online (Sandbox Code Playgroud)
另请注意,在活动目录中,每个属性都可以有多个值(如MemberOf字段),因此您也必须迭代它们.
我写了一个类似的方法,但我选择了一个List带键/值(它似乎比WCF更易于管理.ILookup将是最佳的,但我不能让它在这里工作).在这里,它是从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));
}
Run Code Online (Sandbox Code Playgroud)
Mag*_*son 11
您可以通过以下方式遍历所有属性:
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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
这就是你需要的吗?
| 归档时间: |
|
| 查看次数: |
33592 次 |
| 最近记录: |