62,047
社区成员
发帖
与我相关
我的任务
分享
//
using System.DirectoryServices;
using System.Configuration;
using System.Text.RegularExpressions;
/// <summary>
/// 读取AD用户信息
/// </summary>
/// <param name="ADUsername">用户</param>
/// <param name="ADPassword">密码</param>
/// <param name="domain">域名</param>
/// <returns></returns>
public static SortedList AdUserInfo(string ADUsername, string ADPassword, string domain ,Label lb)
{
DirectorySearcher src;
string ADPath = "LDAP://" + domain;// "ou=总公司,DC=abc,DC=com,DC=cn"; +",ou=总公司"
SortedList sl = new SortedList();
string GroupName = string.Empty;
GroupName = "总公司";
DirectoryEntry de = new DirectoryEntry(ADPath, ADUsername, ADPassword);
//string domainFilter=ConfigurationManager.AppSettings["DomainFilter"].ToString();
//string domainFilter=System.Configuration.ConfigurationManager.AppSettings["DomainFilter"];
string domainFilter="";
if (domainFilter != string.Empty || domainFilter != "")
{
domainFilter = domainFilter.Replace("_", "&"); //("(&(objectCategory=person)(objectClass=user))");
src = new DirectorySearcher(domainFilter);
}
else
{
src = new DirectorySearcher();
}
src.SearchRoot = de;
src.PageSize = 10000;// 此参数可以任意设置,但不能不设置,如不设置读取AD数据为0~999条数据,设置后可以读取大于1000条数据。
// src.SizeLimit = 2000;
src.SearchScope = SearchScope.Subtree;
try
{
foreach (SearchResult res in src.FindAll()) // foreach (SearchResult res in src.FindAll()) foreach (DirectoryEntry res in de.Children)
{
//if (res.GetDirectoryEntry().Properties["EmailAddress"].Value != "")
//{
//sl.Add(res.GetDirectoryEntry().Properties["Name"].Value, res.GetDirectoryEntry().InvokeGet("Description"));
sl.Add(res.GetDirectoryEntry().Properties["Name"].Value, res.GetDirectoryEntry().Properties["EmailAddress"].Value);
// }
}
}
catch (Exception ex)
{
//AppException oAppException = new AppException("Get Ad Info", ex);
lb.Text = ex.Message;
}
return sl;
}