读书人

.AD 急.获取组里面的所有的用户.U

发布时间: 2012-03-18 13:55:39 作者: rapoo

求助.AD 急.获取组里面的所有的用户.UP也有分!
public SearchResultCollection GetuserConunt(DirectoryEntry de)
{
DirectorySearcher deSearch = new DirectorySearcher();
deSearch.SearchRoot = de;
deSearch.Filter = "(&(objectCategory=person)) ";
deSearch.SearchScope = SearchScope.Subtree;
return deSearch.FindAll();

}

得到的 SearchResultCollection 为空~!很郁闷!!
怎么获取组里面的用户~...求助~!

[解决办法]
断点跟踪调试一下。
[解决办法]
断点跟踪调试一下。
[解决办法]
up
[解决办法]
继续关注
[解决办法]
听说UP也有分
[解决办法]
up
[解决办法]
听说UP有分
[解决办法]
up
[解决办法]
up
[解决办法]
/// <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();
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 "));


// }
}
}
catch (Exception ex)
{
AppException oAppException = new AppException( "Get Ad Info ", ex);
lb.Text = ex.Message;
}
return sl;
}
[解决办法]
domain 的值 类似: ben2005/OU=总公司,DC=abc,DC=com,DC=cn
OU=总公司 ou就是你自己定义的
[解决办法]
会不会是变量生存期或者对象引用的问题?
[解决办法]
调试下嘛..
[解决办法]
调试!调试!我up啊!
[解决办法]
路过...

友情帮顶...
[解决办法]
我也想知道,研究这个问题好久了
[解决办法]
谢谢,up接分
[解决办法]
UP
[解决办法]
up~~~
[解决办法]
up
mark jf, thanks.
[解决办法]
up
[解决办法]
可用另一种方法试试:
DirectoryEntry AD = new DirectoryEntry( "WinNT://192.168.1.23,computer ", ".\\administrator ", "123 ");
DirectoryEntry grp = AD.Children.Find( "users ", "group ");
if (grp.Name != null)
grp.Invoke(函数名, new Object[] {NewUser.Path.ToString()});
// 不好意思,Invoke中的函数名忘了,你可上网查查
// 接下来在此组中遍历用户即可


[解决办法]
//创建AD实例
DirectoryEntry adDomain = new DirectoryEntry( "you domain ");

//查询对象
DirectorySearcher adSearcher = new DirectorySearcher(adDomain);

//过滤语句
adSearcher.Filter = "(&(objectCategory=person)(objectClass=user)) ";

//查询结果
SearchResultCollection srcResult = adSearcher.FindAll();
[解决办法]
首先 获取 组对象 。然后 在对象的属性 member 就ok拉
[解决办法]
获得 组对象然后在 属性 member 里面  得到九 OK 阿拉
[解决办法]
我的代码发出来看看,也许对你有帮助。我用的是Member属性,这个只针对在AD根目录下再建一个组的情况
public ArrayList getGroups()
{
string path = this.serv;
Console.WriteLine( "Start at1 : " + (DateTime.Now).ToLongTimeString());
DirectoryEntry m_obDirEntry = new DirectoryEntry(path);
SearchResultCollection results;
DirectorySearcher srch = new DirectorySearcher(m_obDirEntry);
srch.Filter = "(objectClass=Group) ";

try
{
Console.WriteLine( "Start at2 : " + (DateTime.Now).ToLongTimeString());
results = srch.FindAll();
Console.WriteLine( "Start at3 : " + (DateTime.Now).ToLongTimeString());
}
catch (NotSupportedException ex)
{
//m_strErrors += ex.Message;
//Trace.WriteLine(ex.Message);
srch.Dispose();
return null;
}
catch (Exception ex)


{
//m_strErrors += "\n ";
//m_strErrors += ex.Message;
//Trace.WriteLine(ex.Message);
srch.Dispose();
return null;
}
ArrayList gList = new ArrayList();
try
{
foreach(SearchResult result in results)
{
ResultPropertyCollection propColl = result.Properties;
// this.showProp(propColl);

db.GroupInfo g = new db.GroupInfo();
Console.WriteLine( "Start at4 : " + (DateTime.Now).ToLongTimeString());
if (false == g.Initialize(propColl))
{
Console.WriteLine( "Start at5 : " + (DateTime.Now).ToLongTimeString());
//m_strErrors += "\n ";
//m_strErrors += "Failed to initialize the ADSI object ";
//Trace.WriteLine( "Failed to initialize the ADSI object ");
srch.Dispose();
return null;
}
g.Children = new ArrayList();
// load user
ResultPropertyValueCollection colls = propColl[ "member "];
if (colls != null)
foreach (string adsiU in colls)
{
db.UserInfo u = getUser(adsiU);
if (u != null && u.EMailAddress != null)
g.Children.Add(u);
}
if (g.IsShow)gList.Add(g);
}

return gList;
}
catch (Exception ex)
{
//m_strErrors += "\n ";
//m_strErrors += ex.Message;
//Trace.WriteLine(ex.Message);
srch.Dispose();

return null;
}

}

public db.UserInfo getUser(string ads)
{
string server = this.serv;
try
{
DirectoryEntry ety = new DirectoryEntry(server + "/ " + ads);
//System.Console.WriteLine( "name : " + ety.Name);
//System.Console.WriteLine( "guid : " + ety.Guid);
//System.Console.WriteLine( "type : " + ety.SchemaClassName);
DirectorySearcher searcher = new DirectorySearcher(ety);
searcher.Filter = "(objectClass=User) ";
SearchResult result = searcher.FindOne();
ResultPropertyCollection propColl = result.Properties;
//this.showProp(propColl);
db.UserInfo user = new db.UserInfo();
if (false == user.Initialize(propColl))
{
//m_strErrors += "\n ";
//m_strErrors += "Failed to initialize the ADSI object ";
//Trace.WriteLine( "Failed to initialize the ADSI object ");
ety.Dispose();
return null;
}

return user;
}
catch (Exception exp)
{
return null;
}
}

读书人网 >C#

热点推荐