c# - 读取/过滤事件目录的通讯组子组?

标签 c# sharepoint active-directory distribution active-directory-group

我有一个域为 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/

相关文章:

c# - Linq - 根据字符串列表查询查询结果

ios - 可以从共享点 url 运行 WKWebView 吗?

c# - SSIS 2012 : is it possible to pass references to Package Objects to an external assembly?

c# - ASP.NET 站点和 SharePoint 站点有什么区别?

sharepoint - 谁是 SharePoint 工作流发起人?

asp.net - 事件目录登录

java - 无法使用 Spring LDAP 更新 AD 中的密码和用户帐户控制

Azure辅助角色模拟域帐户

c# - 如何使用 Web 表单上传个人资料图片

c# - 如何结束 Windows 8 动态磁贴的定期更新