查询PrincipalSearcher包含多个字符串 [英] Query PrincipalSearcher for containing multiple strings
问题描述
我希望能够查询活动目录,并给出包含某些单词(如用户"或管理员")的所有组的列表,这是我到目前为止所掌握的
I want to be able to query the active directory give a list of all groups containing certain words like Users or Administrators below is what i've got so far
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
GroupPrincipal qbeGroup = new GroupPrincipal(ctx);
qbeGroup.DisplayName = "Administrators";
qbeGroup.DisplayName = "Users";
PrincipalSearcher srch = new PrincipalSearcher(qbeGroup);
return srch.FindAll().Select(g => g.Name).ToArray();
此代码似乎甚至没有过滤掉不是管理员或用户的组名称.但是无论如何,我不知道如何查询组名是否包含?而不是如果组名等于以及如何对多个字符串执行此操作.
This code doesn't even seem to filter out the the Group names that is not Administrators or Users. But anyway what I can't figure out is how to query if the group name contains? and not if group name is equal to and how to do this of multiple strings.
我可以使用DirectoryEntry做同样的事情,所以我把它放在这里作为参考
I can do the same thing using DirectoryEntry so I put it here for referance
var groups = new List<string>();
var path = string.Format("LDAP://CN=Users,DC=company,DC=com");
var computerEntry = new DirectoryEntry(path);
if (computerEntry != null)
{
using (computerEntry)
{
var userNames =
from DirectoryEntry childEntry
in computerEntry.Children
where childEntry.SchemaClassName == "Group"
select childEntry.Name;
foreach (var name in userNames)
{
if (name.Contains("Administrators") || name.Contains("Users"))
{
groups.Add(name);
}
}
}
}
return groups.ToArray();
推荐答案
我是通过使用foreach循环来完成此操作的,但是以下代码仍未回答我关于如何为Principalsearcher进行操作的问题
I did this by using a foreach loop But the following code still doesn't answer my question on how to do it for Principalsearcher
var groups = new List<string>();
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
GroupPrincipal qbeGroup = new GroupPrincipal(ctx);
PrincipalSearcher srch = new PrincipalSearcher(qbeGroup);
foreach (var group in srch.FindAll())
{
if (group.Name.Contains("Administrators") || group.Name.Contains("Users"))
{
groups.Add(group.Name);
}
}
return groups.ToArray();
这篇关于查询PrincipalSearcher包含多个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!