C# 目录搜索器过滤器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9451618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DirectorySearcher Filter
提问by Marco
When I run this query
当我运行此查询时
// 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();
I get six entries and that's correct.
All records, if I use record.GetDirectoryEntry()have
我得到六个条目,这是正确的。
所有记录,如果我使用record.GetDirectoryEntry()有
distinguishedname: CN=xxx,OU=Utenti,OU=Ingegneria,DC=xxx,DC=xxx
Anyway if I remove comment on distinguishednamepart of the filter, I get zero entries!!
I also tried to use search.PropertiesToLoad.Add("distinguishedname");without luck.
How can I search distinguishednamein filter?
无论如何,如果我删除distinguishedname部分过滤器的评论,我会得到零条目!!
我也尝试在search.PropertiesToLoad.Add("distinguishedname");没有运气的情况下使用。
如何distinguishedname在过滤器中搜索?
UPDATE:
If I try to use "(distinguishedname=*)" +in filter , I still get six records, so I think I can search on distinguishedname...
UPDATE2:
I also tried to use code in Search Active Directory for an OU using a partial path to the OU:
更新:
如果我尝试"(distinguishedname=*)" +在 filter 中使用,我仍然会得到 6 条记录,所以我想我可以搜索专有名称...
更新 2:
我还尝试使用搜索 Active Directory 中的代码使用指向 OU 的部分路径的 OU:
Filter = "(&(objectClass=user)(ou=Ingegneria))";
but I have zero entries (I got two if I remove (objectClass=user)part)
但我有零个条目(如果我删除(objectClass=user)部分,我会得到两个)
采纳答案by marc_s
If you want to query just that then you should bindto that container in your initial connect:
如果您只想查询,那么您应该在初始连接中绑定到该容器:
// 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();
That way, you also massively reduce the space in AD that needs to be searched, thus speeding up your search.
这样,您还可以大量减少 AD 中需要搜索的空间,从而加快搜索速度。
And if you're using .NET 3.5 or newer, you can use a PrincipalSearcherand a "query-by-example" principal to do your searching:
如果您使用的是 .NET 3.5 或更高版本,则可以使用 aPrincipalSearcher和“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....
}
}
If you haven't already - absolutely read the MSDN article Managing Directory Security Principals in the .NET Framework 3.5which shows nicely how to make the best use of the new features in System.DirectoryServices.AccountManagement
如果您还没有 - 绝对阅读 MSDN 文章在 .NET Framework 3.5 中管理目录安全主体,它很好地展示了如何充分利用新功能System.DirectoryServices.AccountManagement

