我有一个域为 myDomain.local
的 Active Directory,其下存在一个包含许多组的Distribution Group
。
我如何(以编程方式)读取所有这些子组以检索其名称列表?
以及如何优化查询来过滤结果,以便只检索以 Region
结尾的所有组?
顺便说一句,我正在使用 C#.Net、ASP.Net 和 sharepoint,而且我对 AD 没有经验。
最佳答案
如果您使用的是 .NET 3.5(或可以升级到它),则可以通过 System.DirectoryServices.AccountManagement
命名空间使用此代码:
// create the "context" in which to operate - your domain here,
// as the old-style NetBIOS domain, and the container where to operate in
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN", "cn=Distribution Group,dc=YourDomain,dc=local");
// define a "prototype" - an example of what you're searching for
// Here: just a simple GroupPrincipal - you want all groups
GroupPrincipal prototype = new GroupPrincipal(ctx);
// define a PrincipalSearcher to find those principals that match your prototype
PrincipalSearcher searcher = new PrincipalSearcher(prototype);
// define a list of strings to hold the group names
List<string> groupNames = new List<string>();
// iterate over the result of the .FindAll() call
foreach(var gp in searcher.FindAll())
{
// cast result to GroupPrincipal
GroupPrincipal group = gp as GroupPrincipal;
// if everything - grab the group's name and put it into the list
if(group != null)
{
groupNames.Add(group.Name);
}
}
这能满足您的需求吗?
有关 System.DirectoryServices.AccountManagement
命名空间的更多信息,请阅读 Managing Directory Security Principals in the .NET Framework 3.5 MSDN 杂志上的文章。
关于c# - 读取/过滤事件目录的通讯组子组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3159424/