gpt4 book ai didi

c# - 目录搜索过滤器

转载 作者:可可西里 更新时间:2023-11-01 08:03:33 26 4
gpt4 key购买 nike

当我运行这个查询时

// Next row is used to login to AD
DirectoryEntry entry = GetEntry(domain, adminUser, adminPassword);
// Here starts the query
DirectorySearcher search = new DirectorySearcher(entry)
{
SearchScope = SearchScope.Subtree,
Filter = "(&" +
"(objectClass=user)" +
// "(distinguishedname=*OU=Ingegneria*)" +
"(givenname=s*)" +
"(samaccountname=*100)" +
")"
};
search.PropertiesToLoad.Add("distinguishedname");
SearchResultCollection result = search.FindAll();

我得到了六个条目,这是正确的。
所有记录,如果我使用 record.GetDirectoryEntry()

distinguishedname: CN=xxx,OU=Utenti,OU=Ingegneria,DC=xxx,DC=xxx

无论如何,如果我删除对过滤器的 distinguishedname 部分的评论,我得到零条目!!
我也尝试过使用 search.PropertiesToLoad.Add("distinguishedname"); 但没有成功。
如何在过滤器中搜索 distinguishedname

更新:
如果我尝试在过滤器中使用 "(distinguishedname=*)"+ ,我仍然会得到 6 条记录,所以我想我可以搜索 distinguishedname...
更新 2:
我还尝试使用 Search Active Directory for an OU using a partial path to the OU 中的代码:

Filter = "(&(objectClass=user)(ou=Ingegneria))";

但是我有零个条目(如果我删除 (objectClass=user) 部分我有两个)

最佳答案

如果您只想查询那个,那么您应该在初始连接中绑定(bind)到该容器:

// Next row is used to login to AD
string ldapPath = "LDAP://OU=Ingegneria,DC=xxx,DC=xxx";
DirectoryEntry searchRoot = GetEntry(ldapPath, adminUser, adminPassword);

// Here starts the query
DirectorySearcher search = new DirectorySearcher(searchRoot)
{
SearchScope = SearchScope.Subtree,
Filter = "(&" +
"(objectClass=user)" +
"(givenname=s*)" +
"(samaccountname=*100)" +
")"
};

search.PropertiesToLoad.Add("distinguishedname");
SearchResultCollection result = search.FindAll();

这样,您还可以大大减少 AD 中需要搜索的空间,从而加快搜索速度。

如果您使用的是 .NET 3.5 或更新版本,您可以使用 PrincipalSearcher 和“query-by-example”主体进行搜索:

// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN", "OU=Ingegneria,DC=xxx,DC=xxx");

// define a "query-by-example" principal - here, we search for a UserPrincipal
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = "s*";
qbeUser.SamAccountName = "*100";

// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

// find all matches
foreach(var found in srch.FindAll())
{
// do whatever here - "found" is of type "Principal"
UserPrincipal userFound = found as UserPrincipal;

if(userFound != null)
{
// do something with your user principal here....
}
}

如果您还没有-绝对阅读 MSDN 文章 Managing Directory Security Principals in the .NET Framework 3.5它很好地展示了如何充分利用 System.DirectoryServices.AccountManagement

中的新功能

关于c# - 目录搜索过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9451618/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com