我最近刚开始学习C#,由于工作上的需要,我正在尝试编写一个方法,该方法将查询Active Directory中的特定OU,并且只查询该OU,而不查询子OU。该方法如下所示:
public List<string> getAllActiveUsers()
{
PrincipalContext oPrincipalContext = GetPrincipalContext();
UserPrincipal oUserPrincipal = new UserPrincipal(oPrincipalContext)
{
Enabled = true
};
PrincipalSearcher oPrincipalSearcher = new PrincipalSearcher(oUserPrincipal);
List<string> allUsers = new List<string>();
foreach (var found in oPrincipalSearcher.FindAll())
{
allUsers.Add(found.DisplayName.ToString());
}
allUsers.Sort();
return allUsers;
}现在的方法只拉取启用了用户的用户帐户,但问题是它拉取subOUs中的帐户,这是不可取的。我已经用谷歌搜索了相当长一段时间,对于这个问题没有真正的答案,我充其量只是一个新手,如果有修改代码的建议,请告诉我最终的方法是什么样子的。
任何和所有的帮助都是感激的。
谢谢!
更新:显然我的谷歌搜索还不够,所以我采取了另一种方法,我遇到了一个建议,使用GetUnderlyingSearcher()可以工作,但我仍然不知道如何使用它。一些额外的研究得出了我需要的结果,以下是更新后的方法:
public List<string> getAllActiveUsers()
{
PrincipalContext oPrincipalContext = GetPrincipalContext();
UserPrincipal oUserPrincipal = new UserPrincipal(oPrincipalContext) { Enabled = true };
PrincipalSearcher oPrincipalSearcher = new PrincipalSearcher(oUserPrincipal);
//Setting the search scope by going down to DirectorySearcher itself, as it's not possible to set this
//via PrincipalSearcher directly
((DirectorySearcher)oPrincipalSearcher.GetUnderlyingSearcher()).SearchScope = SearchScope.OneLevel;
List<string> allUsers = new List<string>();
foreach (var found in oPrincipalSearcher.FindAll())
{
allUsers.Add(found.DisplayName.ToString());
}
allUsers.Sort();
return allUsers;
}谢谢你的建议!
发布于 2013-07-13 02:33:16
这段代码解决了我的问题:
((DirectorySearcher)oPrincipalSearcher.GetUnderlyingSearcher()).SearchScope = SearchScope.OneLevel;最终的方法如下所示:
public List<string> getAllActiveUsers()
{
PrincipalContext oPrincipalContext = GetPrincipalContext();
UserPrincipal oUserPrincipal = new UserPrincipal(oPrincipalContext) { Enabled = true };
PrincipalSearcher oPrincipalSearcher = new PrincipalSearcher(oUserPrincipal);
//Setting the search scope by going down to DirectorySearcher itself, as it's not possible to set this
//via PrincipalSearcher directly
((DirectorySearcher)oPrincipalSearcher.GetUnderlyingSearcher()).SearchScope = SearchScope.OneLevel;
List<string> allUsers = new List<string>();
foreach (var found in oPrincipalSearcher.FindAll())
{
allUsers.Add(found.DisplayName.ToString());
}
allUsers.Sort();
return allUsers;
}发布于 2013-07-12 02:22:10
嗯,我不熟悉PrincipalSearcher,但您可以尝试使用DirectorySearcher。它有一个选项SearchScope,允许您设置OneLevel或SubTree (或Base)。下面是一些您可以尝试的示例代码(这是从我的产品代码中拆分出来的,因此它未经测试,甚至可能无法编译,但它应该会给您一些启发):
DirectorySearcher dsr = new DirectorySearcher();
dsr.SearchRoot = new DirectoryEntry(settings.Path, settings.Username, settings.Password, settings.AuthType);
dsr.PageSize = 100;
dsr.SizeLimit = 0;
dsr.SearchScope = SearchScope.OneLevel;
dsr.Filter = "(&(objectclass=user)(sn=UserLastName))";
dsr.PropertiesToLoad.AddRange(new string[] { "sn", "givenName" });
using (SearchResultCollection src = dsr.FindAll())
{
foreach (SearchResult sr in src)
{
string propName = lp.Name;
ResultPropertyValueCollection rpvc = sr.Properties[propName];
string val = (string)rpvc[0];
}
}https://stackoverflow.com/questions/17600347
复制相似问题