2
我希望能夠查詢活動目錄,得到含像用戶或管理員低於某些詞的所有組的列表是我到目前爲止已經查詢PrincipalSearcher包含多個字符串
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();
這段代碼有甚至不會過濾掉不是管理員或用戶的組名。但無論如何,我無法弄清楚如何查詢組名是否包含?而不是如果組名等於以及如何做到這一點的多個字符串。
我能做使用的DirectoryEntry同樣的事情,所以我把它放在這裏的全球化志願服務青年
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();