c# - 如何在具有多棵树的 AD 林中的全局目录中搜索用户

标签 c# dns active-directory userprincipal

我有以下包含两棵树的 AD 林:

  1. 域 1。有两个子域 Domain2 和 Domain3
  2. 域 4。没有子域。

Domain1 的 DNS 名称是 domain1.local。 Domain4 的 DNS 名称是 domain4.local

在每个域中都有一个启用了全局目录的域 Controller 。

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

我使用以下代码:

// 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");

在我的例子中,principal 为 null(未找到用户)。

在一棵树(domain1 及其子树)中搜索可以很好地使用上面的代码片段,但我不知道如何修改 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);
    }
}

...

关于c# - 如何在具有多棵树的 AD 林中的全局目录中搜索用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18694799/

相关文章:

c# - WPF 绑定(bind) : How to bind a name from a list of filepaths to text of TextBlock in ListBox?

C#:SerialPort.Open 超时?

language-agnostic - 如何检查域名是否存在?

java - 从 Activity 目录获取帐户到期日期

perl - 从 1000 增加限制?

时间:2019-03-17 标签:c#(Xna)ErrorCode : CA1405 "Game1" is marked ComVisible(true) but has the following ComVisible(false) types in its object hierarchy: 'Game'

c# - MVC 4 - 更改密码错误 : "Index (zero based) must be greater than or equal to zero..."

dns - 将我的域名指向亚马逊实例

ssh - 通过socks代理的DNS。如何更改域解析的 Windows 设置。

c# - 为什么在 active directory 组中不能创建为 groupType = Local