gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:20:39 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com