查询Active directory以获取部门名称字符串列表的最简单方法是什么?例如:“财务”、“市场营销”、"IT“等。我的案例是一个拥有3000多个用户的企业的活动目录。
发布于 2009-06-28 23:49:43
假设您只想获得返回了Department属性的对象列表,那么可以在System.DirectoryServices名称空间中使用DirectorySearcher。
那么你的过滤器应该是这样的:
ds.Filter = "(objectClass=user)";然后,您可以告诉搜索者只加载部门属性:
ds.PropertiesToLoad.Add("department");然后枚举结果集:
SearchResultCollection results = ds.FindAll();然后,将每个部门属性添加到字典中,以获取所有唯一值
foreach (SearchResult result in results)
{
string dept = String.Empty;
DirectoryEntry de = result.GetDirectoryEntry();
if (de.Properties.Contains("department"))
{
dept = de.Properties["department"][0].ToString();
if (!dict.ContainsKey(dept))
{
dict.Add(result.Properties["department"][0].ToString();
}
}
}或者,还有命令行工具可以为您提供此信息,如dsquery或adfind。
adfind -default -f "(objectclass=user)" department -list | sort将为您提供所有用户的部门属性的排序列表。
https://stackoverflow.com/questions/1056038
复制相似问题