C# 如何使用C获取Active directory中的部门列表#

C# 如何使用C获取Active directory中的部门列表#,c#,ldap,C#,Ldap,查询Active directory以获取部门名称字符串列表的最简单方法是什么。例如:“金融”、“营销”、“IT”等。 我的案例是一家拥有3000多用户的企业的active directory 假设您只想获取返回Department属性的对象列表,则可以在System.DirectoryServices命名空间中使用DirectorySearcher 那么您的过滤器将类似于: ds.Filter = "(objectClass=user)"; 然后,您可以告诉搜索者只需加载department

查询Active directory以获取部门名称字符串列表的最简单方法是什么。例如:“金融”、“营销”、“IT”等。
我的案例是一家拥有3000多用户的企业的active directory

假设您只想获取返回Department属性的对象列表,则可以在System.DirectoryServices命名空间中使用DirectorySearcher

那么您的过滤器将类似于:

ds.Filter = "(objectClass=user)";
然后,您可以告诉搜索者只需加载department属性:

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

将为您提供所有用户的部门属性的排序列表。

假设您只想获取返回部门属性的对象列表,您可以在System.DirectoryServices命名空间中使用DirectorySearcher

那么您的过滤器将类似于:

ds.Filter = "(objectClass=user)";
然后,您可以告诉搜索者只需加载department属性:

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

将为所有用户提供部门属性的排序列表。

adfind是“Windows操作系统远程服务器管理工具(RSAT)”的一部分。adfind是“Windows操作系统远程服务器管理工具(RSAT)”的一部分,它提供了这些工具的改进版。免费软件提供了这些工具的改进版本