using System.DirectoryServices;
IList<ADEntity> entityList = new List<ADEntity>();//保存实体的List集合,作为以后操作的数据源使用
//根据当前域来读取OU=xxx下的所有用户
public IList<ADEntity> ReadAD()
{
try
{
System.DirectoryServices.DirectoryEntry entry = new System.DirectoryServices.DirectoryEntry();
//entry.Children.Find只能读取下一集如果要下下级要在后面继续加Children.Find
System.DirectoryServices.DirectoryEntry ou = entry.Children.Find("OU=xxx");
System.DirectoryServices.DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(ou);
mySearcher.Filter = ("(objectClass=user)"); //user表示用户
foreach (System.DirectoryServices.SearchResult resEnt in mySearcher.FindAll())
{
}
}
catch (Exception ex)
{
}
finally {
}
return entityList;
}