如何过滤包含特定用户的组的LDAP查询?

Stu*_*ley 8 ldap active-directory

如何将Active Directory LDAP查询过滤到包含经过身份验证/绑定的用户(或任何用户)的组?这很好用:

(&(objectClass=group)(member=*))
>>> lots of results
Run Code Online (Sandbox Code Playgroud)

但我不能再详细说明了:

(&(objectClass=group)(member=*S*))
>>> nothing
Run Code Online (Sandbox Code Playgroud)

MSDN提到使用这样的过滤器:

(member:1.2.840.113556.1.4.1941:=(cn=user1,cn=users,DC=x))
Run Code Online (Sandbox Code Playgroud)

但即使忽略了涉及的疯狂超幻数,当我尝试用它过滤时,我总是得到0结果(甚至用cn=user1,cn=users,DC=x我自己的distinguishedName替换,甚至替换它*).

Ray*_*und 8

您需要用户的完整DN,即

(&(member=CN=Your Name,OU=Your OU,DC=company,DC=com)(objectClass=group))
Run Code Online (Sandbox Code Playgroud)

注意你不能在这个中使用*

  • "当我尝试用它过滤时,我总是得到0结果(**甚至用我自己的distinguishedName替换`cn = user1,cn = users,DC = x`**" (4认同)

JPB*_*anc 5

因此,“ 搜索过滤器语法”中介绍了递归搜索中涉及的疯狂的超魔术数字

要在一个搜索中(递归)查找“ user1”是其成员的所有组:

  • 将基础设置为组容器DN;例如根DN(dc = dom,dc = fr)
  • 将范围设置为子树
  • 使用以下过滤器: (member:1.2.840.113556.1.4.1941:=cn=user1,cn=users,DC=x)

使用LDIFDE.EXE明确显示的Windows Server中包含的命令行工具,它提供:

ldifde -f user1Grps.ldf -d "dc=societe,dc=local" -r "(member:1.2.840.113556.1.4.1941:=cn=user1,ou=Monou,dc=societe,dc=local)"
Run Code Online (Sandbox Code Playgroud)

如果在W2K8或W2K8 R2服务器上运行该服务器,请小心以管理员身份运行。

如果您使用C#编程,则可以使用:

/* 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);    
}
Run Code Online (Sandbox Code Playgroud)