5

我正在尝试编写一个 LDAP 查询,它将发现用户是否是与通配符查询匹配的组的成员,并且我正在尝试使用 LDAP_MATCHING_RULE_IN_CHAIN OID 来执行此操作。我基本上遵循此页面上的示例 2:

http://support.microsoft.com/kb/914828

我发现这种方法在域中运行良好,即如果 user1 在 group1 中并且 group1 在 group2 中,那么我可以编写一个匹配“*2”的查询,LDAP 查询将找到嵌套关系并将用户与组匹配.

但是,现在我被要求支持同一森林中域之间的关系。所以现在我有:

  • user1 是域 1 中 group1 的成员
  • 域 1 中的 group1 是域 2 中 group2 的成员

而且我希望能够将 user1 与 group2 进行匹配......我不知道如何让 LDAP_MATCHING_RULE_IN_CHAIN 做到这一点:

我尝试将查询的基础设置为以下内容:

  1. 域 1,但这只是返回域 1 中的组
  2. 域 1 和域 2 的父域,但这不返回任何结果。
  3. GC,通过查询“rootDSE”属性找到,但这只是返回域 1(即 GC 服务器)内的组

任何人都知道我怎样才能使这项工作?

4

1 回答 1

3

据我了解,这样做的一种方法是:

  1. 从 RootDSE 中,查找配置 NamingContext。
  2. 在配置 NamingContext 中寻找具有现有crossRef属性的类对象。nETBIOSName
  3. 从这些条目中,使用您通过 usingdnsRootnCNameattributs 描述的算法。工作林 DNS 允许您加入dnsRoot. nCName允许从根目录搜索。

作为企业管理员组的成员,请小心执行此操作。

这是代码的示例。

/* Retreiving RootDSE
 */
string ldapBase = "LDAP://WM2008R2ENT:389/";
string sFromWhere = ldapBase + "rootDSE";
DirectoryEntry root = new DirectoryEntry(sFromWhere, "dom\\jpb", "PWD");
string configurationNamingContext = root.Properties["configurationNamingContext"][0].ToString();

/* Retreiving the root of all the domains
 */
sFromWhere = ldapBase + configurationNamingContext;
DirectoryEntry deBase = new DirectoryEntry(sFromWhere, "dom\\jpb", "PWD");

DirectorySearcher dsLookForDomain = new DirectorySearcher(deBase);
dsLookForDomain.Filter = "(&(objectClass=crossRef)(nETBIOSName=*))";
dsLookForDomain.SearchScope = SearchScope.Subtree;
dsLookForDomain.PropertiesToLoad.Add("nCName");
dsLookForDomain.PropertiesToLoad.Add("dnsRoot");

SearchResultCollection srcDomains = dsLookForDomain.FindAll();

foreach (SearchResult aSRDomain in srcDomains)
{
  /* For each root look for the groups containing my user
   */
  string nCName = aSRDomain.Properties["nCName"][0].ToString();
  string dnsRoot = aSRDomain.Properties["dnsRoot"][0].ToString();

  /* To find all the groups that "user1" is a member of :
   * Set the base to the groups container DN; for example root DN (dc=dom,dc=fr) 
   * Set the scope to subtree
   * Use the following filter :
   * (member:1.2.840.113556.1.4.1941:=cn=user1,cn=users,DC=x)
   */
  /* Connection to Active Directory
   */
  sFromWhere = "LDAP://" + dnsRoot + "/" + nCName;
  deBase = new DirectoryEntry(sFromWhere, "dom\\jpb", "PWD");

  DirectorySearcher dsLookFor = new DirectorySearcher(deBase);
  // you cancomplete the filter here  (&(member:1.2.840.113556.1.4.1941:=CN=user1 Users,OU=MonOu,DC=dom,DC=fr)(cn=*2)
  dsLookFor.Filter = "(member:1.2.840.113556.1.4.1941:=CN=user1 Users,OU=MonOu,DC=dom,DC=fr)";
  dsLookFor.SearchScope = SearchScope.Subtree;
  dsLookFor.PropertiesToLoad.Add("cn");

  SearchResultCollection srcGroups = dsLookFor.FindAll();

  foreach (SearchResult srcGroup in srcGroups)
  {
    Console.WriteLine("{0}", srcGroup.Path);
  }
}

这只是一个概念证明,您必须完成:

使用using(){}表单处理 DirectoryEntry 对象

异常管理


已编辑 (2011-10-18 13:25)

您可以在System.DirectoryServices.AccountManagement Namespace中给出的方法中找到您对解决问题方式的评论。这是一种递归解决方案。这一次,我使用属于 group2 (在第三个域中)的 group1 (在另一个域中)的用户进行测试,它似乎可以工作。

/* Retreiving a principal context
 */
Console.WriteLine("Retreiving a principal context");
PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, "WM2008R2ENT:389", "dc=dom,dc=fr", "jpb", "PWD");


/* Look for all the groups a user belongs to
 */
UserPrincipal aUser = UserPrincipal.FindByIdentity(domainContext, "user1");
PrincipalSearchResult<Principal> a =  aUser.GetAuthorizationGroups();

foreach (GroupPrincipal gTmp in a)
{
  Console.WriteLine(gTmp.Name);    
}
于 2011-10-17T14:05:09.193 回答