LDAP查询实例

时间:2021-04-10 07:46:10
 
 /// <summary>
/// 搜索AD人员
/// </summary>
/// <param name="keyWords">搜索部门关键字</param>
/// <returns>AD人员集合</returns>
public List<DomainUser> SearchPersonByDeptName(string keyWords)
{
List<DomainUser> ReturnList = SearchAdObjectCache(new DomainConfig(Constants.C_SEARCH_ByDeptName, ldapPath), keyWords).ConvertAll(new Converter<DomainObject, DomainUser>(O2User));
return ReturnList;
}
 public static readonly string C_SEARCH_ByDeptName = "(department={0}*)";
 internal struct DomainConfig
{
static DomainConfig()
{
string ServerUrl = "LDAP://域";
if (string.IsNullOrEmpty(ServerUrl))
{
_activeObject = Constants.C_ACTIVE_OBJECT;
}
else
{
_activeObject = ServerUrl;
}
}
private static string _activeObject;
public static string ActiveObject
{
get { return _activeObject; }
set { _activeObject = value; }
} private string _searchConditon;
public string SearchConditon
{
get { return _searchConditon; }
set { _searchConditon = value; }
}
string ldapPath ;
public DomainConfig(string sCodition, string LDAPPath)
{
_searchConditon = sCodition;
ldapPath = LDAPPath;
}
public DirectoryEntry[] ADEntrysGet()
{
string[] Paths=ldapPath.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
DirectoryEntry[] DEs = new DirectoryEntry[Paths.Length];
for (int i = ; i < Paths.Length;i++ )
{
DirectoryEntry deRoot;
string ADUser = string.Empty;
string ADPassword = string.Empty;
if (string.IsNullOrEmpty(ADUser))
{
deRoot = new DirectoryEntry(Paths[i]);
}
else
{
deRoot = new DirectoryEntry(Paths[i], ADUser, ADPassword);
}
DEs[i]=deRoot;
}
return DEs;
}
}
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 > )
{
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;
}