62,046
社区成员
发帖
与我相关
我的任务
分享
private void Button1_Click(object sender, System.EventArgs e)
{
string path = "";
path = "GC://OU=信息中心,OU=XXX,DC=XXX,DC=com,DC=cn";
DirectoryEntry dir = new DirectoryEntry(path,"xxx","xxx");
this.GetUser(dir);
}
private void GetUser(DirectoryEntry dir)
{
foreach(DirectoryEntry childDir in dir.Children)
{
if(dir.Children == null)
{
return;
}
Response.Write(childDir.Properties["name"].Value.ToString()+"<br>");
this.GetUser(childDir);
}
}
string path = "";
path = "GC://OU=ERP咨询部,OU=信息中心,OU=xxx,DC=xxx,DC=com,DC=cn";
DirectoryEntry dir = new DirectoryEntry(path,"user","pwd");
foreach(DirectoryEntry childDir in dir.Children)
{
Response.Write(childDir.Path+"*"+childDir.Properties["mail"].Value.ToString() +"<br>");
Response.Write(childDir.Path+"*"+childDir.Properties["name"].Value.ToString() +"<br>");
}
/// <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;
}
public DataSet GetUsersByGroupAndOU(string testGroup,string ou)
{
SearchResultCollection src = Searcher("LDAP://DC=xxx,DC=com,CN="+testgroup, "&(objectClass=user)(ou=" + ou + ")");
DataSet ds = new DataSet();
ds.Tables.Add("UserByOU");
ds.Tables[0].Columns.Add("name");
foreach (SearchResult sr in src)
{
string cn = "";
DirectoryEntry entry = sr.GetDirectoryEntry();
if (entry.Properties["name"].Value != null)
cn = entry.Properties["name"].Value.ToString();
string[] dr = new String[] { cn };
ds.Tables[0].Rows.Add(dr);
}
return ds;
}