使用GUID跨C#中的多个林搜索Active Directory对象

时间:2013-02-07 05:16:25

标签: c# search guid

我正在解决一个问题,即我要使用GUID在两个不同的森林中搜索OU的distinguishedName属性。例如,

  1. domain1.local是一个林(只有一个域),其中创建'XYZ' OU对象,其GUID为xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx。另外,假设domain1_machine是domain1.local forest中存在的计算机。

  2. domain2.local是一个林(只有一个域),其中创建了'ABC' OU对象,其GUID为yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy。另外,假设domain2_machine是domain2.local forest中存在的计算机。

  3. 请注意,这两个森林之间存在信任关系:

    • 信任方向:双向
    • 信托类型:森林

    以下代码返回正确的'XYZ'OU的distinguishedName,如果从domain1_machine运行,其中GUID = xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx,但抛出“ 服务器上没有此类对象。 “当GUID = yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyyy时。当从domain2_machine机器运行相同的代码时,它在查询'ABC'时成功但对'XYZ'OU失败。

    请注意,此示例在两台计算机的域管理员帐户下运行。

    Guid[] ou = { new Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"), new Guid("yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy") };
    
    Forest f = Forest.GetCurrentForest();
    
    foreach (Guid guid in ou)
    {
       DirectoryEntry de = null;
       string result = null;
    
       try
       {
          de = f.RootDomain.GetDirectoryEntry();
          de.Path = "LDAP://<GUID=" + guid + ">";
          result = de.Properties["distinguishedName"].Value.ToString();
       }
       catch (Exception e)
       {
          Console.WriteLine(e.Message);
       }
    
       Console.WriteLine(result);
    }
    

    我发现this文章相关但无法从中找出我的解决方案。我确实尝试过其他方式(从domain1_machine运行)但抛出异常yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyyy guid:错误:服务器上没有这样的对象。但是为xxxxxxxx返回正确的distinguishedName -xxxx-XXXX-XXXX-XXXXXXXXXXXX。

    Forest f = Forest.GetForest(new DirectoryContext(DirectoryContextType.Forest, "domain2.local"));
    
    Forest f = Forest.GetForest(new DirectoryContext(DirectoryContextType.Forest, "domain2.local", username, password));
    

    我已经在5日向Microsoft forum发布了同样的问题,但目前还没有任何回复。

    如果我遗失了什么,请告诉我。

0 个答案:

没有答案
相关问题