C# 如何在具有多个树的广告林中搜索全局目录中的用户

C# 如何在具有多个树的广告林中搜索全局目录中的用户,c#,dns,active-directory,userprincipal,C#,Dns,Active Directory,Userprincipal,我有以下两棵树的广告林: 域1。有两个子域Domain2和Domain3 域4。没有子域。 域1的DNS名称为Domain1.local。域4的DNS名称为Domain4.local 在每个域中都有一个启用全局编录的域控制器 我正试图通过用户的SID从域4获取用户的UserPrincipal。该程序从域2中的机器运行 我使用以下代码: // Running on some machine from Domain2 PrincipalContext context = new PrincipalC

我有以下两棵树的广告林:

域1。有两个子域Domain2和Domain3 域4。没有子域。 域1的DNS名称为Domain1.local。域4的DNS名称为Domain4.local

在每个域中都有一个启用全局编录的域控制器

我正试图通过用户的SID从域4获取用户的UserPrincipal。该程序从域2中的机器运行

我使用以下代码:

// Running on some machine from Domain2
PrincipalContext context = new PrincipalContext(
    ContextType.Domain,
    "dc2.domain2.domain1.local:3268", // Using Global Catalog port and local domain controller
    "DC=domain1, DC=local", // I guess the problem is here
    "domain1\\super-admin", // User has all necessary rights across all domains 
    "password");

UserPrincipal principal = UserPrincipal.FindByIdentity(context, "SID-OF-A-USER-FROM-DOMAIN-4");
在我的例子中,主体为null,未找到用户

在一个树域1及其子域中搜索可以很好地处理上面的代码片段,但我不知道如何修改PrincipalContext构造函数的容器参数,以真正启用全林搜索

起初我以为DC=domain1,DC=local指向森林根,但这里似乎有误解

我知道,如果我将容器路径更改为DC=domain4,DC=local,那么搜索将起作用,但只适用于domain4中的用户

但我确实需要这样一个指向整个林的容器路径,这样我就可以使用相同的PrincipalContext从林中的任何域搜索用户


非常感谢您的帮助,特别是如果有人能够澄清我的要求是否可以实现。

除了切换到DirectorySearcher,我们找不到任何其他解决方案。所以PrincipalContext类似乎不完全支持在整个林中搜索

我不能说这个解决方案是理想的。我想它可以调整以获得更好的性能。但我们真的很失望,这不能用PrincipalContext完成

下面是我们的代码现在的工作原理:

...

// Here is a list of SIDs of users we want to find (initialized somewhere above)
List<string> userSids;

// List of sample results.
List<string> loadedUsers = new List<string>();

using (DirectorySearcher searcher = new DirectorySearcher(new DirectoryEntry("GC://dc2.domain2.domain1.local")))
{
    StringBuilder filterStringBuilder = new StringBuilder();

    // Just create a single LDAP query for all user SIDs
    filterStringBuilder.Append("(&(objectClass=user)(|");
    foreach (string userSid in users)
    {
        filterStringBuilder.AppendFormat("({0}={1})", "objectSid", userSid);
    }

    filterStringBuilder.Append("))");

    searcher.PageSize = 1000; // Very important to have it here. Otherwise you'll get only 1000 at all. Please refere to DirectorySearcher documentation

    searcher.Filter = filterStringBuilder.ToString();

    // We do not want to go beyond GC
    searcher.ReferralChasing = ReferralChasingOption.None;

    searcher.PropertiesToLoad.AddRange(
        new[] { "DistinguishedName" });

    SearchResultCollection results = searcher.FindAll();

    foreach (SearchResult searchResult in results)
    {
        string distinguishedName = searchResult.Properties["DistinguishedName"][0].ToString();
        loadedUsers.Add(distinguishedName);
    }
}

...

我现在面临着一个类似的问题:我和你有着相似的要求,这对我来说很有效