【问题标题】:Ldap Query for all members specific to a GroupLdap 查询特定于组的所有成员
【发布时间】:2016-01-06 22:02:25
【问题描述】:

我希望将属于特定组“groupName”的用户列表传递到私有方法中。

 DirectoryEntry de = new DirectoryEntry("LDAP://DC=xxxx,DC=net"); // Root Directory //
 var ds = new DirectorySearcher(de);
 ds.PropertiesToLoad.Add("SAMAccountName");
 ds.PropertiesToLoad.Add("member");
 ds.Filter = "(&(objectClass=group)(SAMAccountName=" + groupName + "))";
 SearchResultCollection AllGroupUsers;     
 AllGroupUsers = ds.FindAll();

查询返回 3 个属性:- adspath、accountName 和 member。 成员是我真正追求的。我访问成员属性及其值,如以下代码所示:-

 if (AllGroupUsers.Count > 0)
   {
     ResultPropertyValueCollection values = AllGroupUsers[0].Properties["member"];

但是这里发生了一些奇怪的事情。在等号的 RHS 上,AllGroupUsers 具有特定成员的值为“CN=Mike Schomaker R,........”

在等号的 LHS 上,值具有“CN=Mike Schoomaker (OR),.....”

我不太确定这是怎么可能的... AllGroupUsers 下的每个值都不会发生这种情况...我只知道活动目录上的外部用户会发生这种情况...任何人都可以显示我该如何解决这个问题并获得实际的 firstName、LastName 和 MiddleInitial?

【问题讨论】:

标签: c# asp.net active-directory ldap


【解决方案1】:
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
    // find a user
    using (var group = GroupPrincipal.FindByIdentity(ctx, "groupName"))
    {
        if (group == null)
        {
            MessageBox.Show("Group does not exist");
        }
        else
        {
            var users = group.GetMembers(true);
            foreach (UserPrincipal user in users)
            {
                //user variable has the details about the user 
            }
         } 
      }
}

【讨论】:

  • 您可以对 GroupName 执行相同操作,我将编辑并显示一秒钟
  • 我正在尝试解决一个难题,而不是让它正常工作...我已经看过您发布的示例。谢谢
  • 谢谢MethodMan;这正是我一直在寻找的,并且非常适合抓取属于特定广告组的用户;不知道为什么这不是公认的答案,但你得到了我的投票!谢谢。
【解决方案2】:

要获取用户,而不是组,您应该设置 DirectoryEntry 对象并使用相应的属性(例如 displayNamesngivenNameinitials

例子:

...    
AllGroupUsers = ds.FindAll();
if (AllGroupUsers.Count > 0) {
    ResultPropertyValueCollection values = AllGroupUsers[0].Properties["member"];
    foreach (string s in values) 
    {
        DirectoryEntry u = new DirectoryEntry("LDAP://" + s);  
        Console.WriteLine(u.Properties["displayName"].Value);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多