域用户列表

chengpei_chen 2008-07-31 02:23:18
请问用过用.net代码怎么取出域服务器中所有域中的用户名?
...全文
118 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
gongsun 2008-08-01
  • 打赏
  • 举报
回复
如此之后我只能接分了...
sportdog 2008-08-01
  • 打赏
  • 举报
回复
我的部门-->三个小组-->具体人员

以上代码测试通过,需using System.DirectoryServices;。

楼主自己试试吧,
sportdog 2008-08-01
  • 打赏
  • 举报
回复
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);
}
}
sportdog 2008-08-01
  • 打赏
  • 举报
回复
昨天看到这个帖子觉得有点意思,就回去查了下资料,以上代码测试通过可以运行,不过有如下问题。

A:path是叶子节点。
否则childDir.Properties["mail"].Value会抱错。
我不知道如何判断何时才是叶子节点。







sportdog 2008-08-01
  • 打赏
  • 举报
回复
			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>");
}
紫气东来_999 2008-07-31
  • 打赏
  • 举报
回复

/// <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;
}



s208ping 2008-07-31
  • 打赏
  • 举报
回复
没弄过,友情帮顶,要给分啊
wxdxy 2008-07-31
  • 打赏
  • 举报
回复
帮顶吧
jl_lsj 2008-07-31
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sportdog 的回复:]
mark
[/Quote]
sportdog 2008-07-31
  • 打赏
  • 举报
回复
mark
眼里进了砂 2008-07-31
  • 打赏
  • 举报
回复

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;
}

没测试过,具体的你可以自己试一下,查询语句参考LDAP,或者自己找个域控可以动态创建查询字符串的。
chengpei_chen 2008-07-31
  • 打赏
  • 举报
回复
没人帮忙啊,分不够可以再加

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧