1 /// <summary> 2 /// 搜索AD人员 3 /// </summary> 4 /// <param name="keyWords">搜索部门关键字</param> 5 /// <returns>AD人员集合</returns> 6 public List<DomainUser> SearchPersonByDeptName(string keyWords) 7 { 8 List<DomainUser> ReturnList = SearchAdObjectCache(new DomainConfig(Constants.C_SEARCH_ByDeptName, ldapPath), keyWords).ConvertAll(new Converter<DomainObject, DomainUser>(O2User)); 9 return ReturnList; 10 }
public static readonly string C_SEARCH_ByDeptName = "(department={0}*)";
1 internal struct DomainConfig 2 { 3 static DomainConfig() 4 { 5 string ServerUrl = "LDAP://域"; 6 if (string.IsNullOrEmpty(ServerUrl)) 7 { 8 _activeObject = Constants.C_ACTIVE_OBJECT; 9 } 10 else 11 { 12 _activeObject = ServerUrl; 13 } 14 } 15 private static string _activeObject; 16 public static string ActiveObject 17 { 18 get { return _activeObject; } 19 set { _activeObject = value; } 20 } 21 22 private string _searchConditon; 23 public string SearchConditon 24 { 25 get { return _searchConditon; } 26 set { _searchConditon = value; } 27 } 28 string ldapPath ; 29 public DomainConfig(string sCodition, string LDAPPath) 30 { 31 _searchConditon = sCodition; 32 ldapPath = LDAPPath; 33 } 34 public DirectoryEntry[] ADEntrysGet() 35 { 36 string[] Paths=ldapPath.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); 37 DirectoryEntry[] DEs = new DirectoryEntry[Paths.Length]; 38 for (int i = 0; i < Paths.Length;i++ ) 39 { 40 DirectoryEntry deRoot; 41 string ADUser = string.Empty; 42 string ADPassword = string.Empty; 43 if (string.IsNullOrEmpty(ADUser)) 44 { 45 deRoot = new DirectoryEntry(Paths[i]); 46 } 47 else 48 { 49 deRoot = new DirectoryEntry(Paths[i], ADUser, ADPassword); 50 } 51 DEs[i]=deRoot; 52 } 53 return DEs; 54 } 55 }
public static readonly string C_ACTIVE_OBJECT = "LDAP://域";
/// </summary> /// <param name="dc">搜索目标和条件</param> /// <param name="keyWords">关键字</param> /// <returns>AD对象集合</returns> private static List<DomainObject> SearchAdObjectCache(DomainConfig dc, string keyWords) { List<DomainObject> ReturnList = new List<DomainObject>(); object CacheObject; string CacheName = "DomainSearchAdObjectCache" + dc.SearchConditon + keyWords; if (string.IsNullOrEmpty(keyWords) || keyWords.Length < KeyWordLimitLength) { } else { CacheObject = CacheHelper.Get(CacheName); if (CacheObject != null) { ReturnList = (List<DomainObject>)CacheObject; } else { ReturnList = SearchAdObject(dc, keyWords); CacheObject = CacheHelper.Get(CacheName); if (CacheObject == null && ReturnList != null) { CacheHelper.Insert(CacheName, ReturnList, CacheDuration); } } } return ReturnList; }
/// <summary> /// AD对象搜索 /// </summary> /// <param name="dc">搜索目标和条件</param> /// <param name="keyWords">关键字</param> /// <returns>AD对象集合</returns> private static List<DomainObject> SearchAdObject(DomainConfig dc, params object[] keyWords) { List<DomainObject> ObjectList = new List<DomainObject>(); DirectoryEntry[] DEs = dc.ADEntrysGet(); foreach (DirectoryEntry DE in DEs) { //建立ActiveDirectory绑定 using (DirectoryEntry deRoot = DE) { //建立ActiveDirectory搜索类 using (DirectorySearcher ds = new DirectorySearcher(deRoot)) { //搜索条件 ds.Filter = string.Format(dc.SearchConditon, keyWords); //设置返回条目 //ds.SizeLimit = 40; //搜索结果 SearchResultCollection srCollection = ds.FindAll(); //如果没有结果,返回空数据 if (srCollection != null && srCollection.Count > 0) { ADUser dUser = null; ADGroup dGroup = null; foreach (SearchResult sr in srCollection) { if (GetProperty(Constants.AD_USER_objectcategory, sr.Properties).ToString().ToLower().Contains("person")) { dUser = new ADUser(); dUser = ADUserSet(sr.Properties); ObjectList.Add(dUser); } else { dGroup = new ADGroup(); dGroup = ADGroupSet(sr.Properties); ObjectList.Add(dGroup); } } } } } } return ObjectList; }