C# Active Directory显示表中的所有属性
我正在尝试实现一个LDAP查询,以收集我们关于用户的所有属性,而无需事先指定属性,我想在下面代码使用的表格中显示这一点。如果我取消对search.PropertiesToLoad.Add(“cn”)的注释,这是可行的;行,并将以相同的方式显示我添加的任何其他属性,但在对所有属性进行完整搜索时不会显示C# Active Directory显示表中的所有属性,c#,active-directory,C#,Active Directory,我正在尝试实现一个LDAP查询,以收集我们关于用户的所有属性,而无需事先指定属性,我想在下面代码使用的表格中显示这一点。如果我取消对search.PropertiesToLoad.Add(“cn”)的注释,这是可行的;行,并将以相同的方式显示我添加的任何其他属性,但在对所有属性进行完整搜索时不会显示 DirectoryEntry myLdapConnection = createDirectoryEntry(); DirectorySearcher search = new DirectoryS
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的情况下循环所有属性,但这不是实现我想要的结果的方法
我知道我需要一些try-catch和代码中的其他位,目前为止,这只是一个粗略的草稿。您可以通过以下方式迭代所有属性:
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());
}
}
}
这就是您需要的吗?这可以使用
DirectoryEntry
完成,但我认为SearchResultCollection
没有所有字段。尝试为每个搜索结果创建一个
DirectoryEntry
,它应该具有所有active directory属性:
DirectoryEntry entry = result.GetDirectoryEntry();
另外,请注意,在active directory中,每个属性都可以有多个值(如MemberOf字段),因此您还必须迭代它们。我编写了一个类似的方法,但我选择了一个带有键/值的
列表
(它似乎比WCF更易于管理。ILookup
将是最佳选择,但我无法让它在这里工作)。在这里,从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));
}
var list=newlist();
foreach(entry.Properties中的PropertyValueCollection属性)
foreach(属性中的对象o)
{
字符串值=o.ToString();
添加(新的KeyValuePair(property.PropertyName,value));
}
在自己研究如何执行此操作时遇到此线程。
相反,我找到了一种不同的方法,似乎工作得很好
return ((DirectoryEntry)UserPrincipal.Current.GetUnderlyingObject()).Properties.PropertyNames;
无论如何,它的加载都可以在组合框中找到。
以防其他人遇到此线程。如果您的评论将UserPrincipal与DirectorySearcher或其他相关内容联系起来,这可能会对我更有帮助。我可能对该对象了解不够,但没有看到有效的对象类型,无法解决此问题。这是使用较新的System.DirectoryServices.AccountManagement。所以您需要添加一个引用并使用语句。