Active Directory 组名称唯一性要求(来自 C# 客户端的查询)

Active Directory group name uniqueness requirement (query from C# client)

我们公司的 C# 产品使用 System.DirectoryServices.AccountManagement 查询 Active Directory 中的用户和组。我们使用以下方法获取本金:

...
PrincipalContext principalContext = new PrincipalContext(ContextType.Domain);
return principalContext;
...

我们使用(例如 groupName = "Devs")获得 Active Directory 组:

...
GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(this.principalContext, groupName);
...

当我们 运行 在一个简单的单域 Active Directory 数据库上使用此设置时,一切正常。

我的问题是,当我们 运行 这段代码在一个有多个 "Devs" 组的大森林中时会发生什么?一个林中可以有多个 "Devs" 安全组吗?如果是,它将如何解析 "Devs"?我是否必须切换到使用方法:

public static GroupPrincipal FindByIdentity(
    PrincipalContext context,
    IdentityType identityType,
    string identityValue
)

我目前无法模拟这个(缺乏资源和时间),我已经阅读了很多关于这个的内容。我知道有本地、全球和通用安全组,分布在域树中。但是森林中的域树在根之间有某种信任,所以它们不会完全不了解彼此。在林中有 "Devs" 个重复项的最坏情况是什么?应用程序如何处理它?

搜索域层次结构是很常见的任务。使用 AccountManagement 类 您可以执行以下操作:

// Connect to global catalog of the forest
var context = new PrincipalContext(ContextType.Domain, "contoso.com:3268", "DC=contoso,DC=com");

// Build a filter principal by name and context
var groupFilter = new GroupPrincipal(context) {Name = "Devs"};

// Build a searcher with a filter applied
var searcher = new PrincipalSearcher(groupFilter);

// This should return all groups in all subdomains matching specified name
var groups = searcher.FindAll().ToList();

foreach (var group in groups)
{
    Console.WriteLine(group.DistinguishedName);
}

您不会有任何重复项,因为在域中不能有超过一个组使用此名称 ("Devs")。在 AccountManagement 术语中,您使用上下文和名称参数创建 GroupPrincipal 对象,并且在上下文中不能有多个具有相同名称的对象。

如果您连接到域控制器 (new PrincipalContext(ContextType.Domain)),则 FindByIdentity 将搜索此单个域。如果您连接到林的全局目录(如我的示例中的端口 3268),则 FindByIdentity 将搜索整个林。 DistinguishedName 属性 将显示组属于哪个域。

至于跨林访问,您需要分别连接到每个林中的全局目录,因为林全局目录之间没有user/group数据复制。