【问题标题】:How to get LDAP nested groups from attribute如何从属性中获取 LDAP 嵌套组
【发布时间】:2019-07-31 12:33:53
【问题描述】:

我可以搜索用户并仅找到用户所属的组。现在我想返回所有组/角色并将用户分配给特定组/角色。

DirectoryEntry 和 PrincipalContext 在我的情况下不起作用,我已经尝试了好几天。

这是我用于搜索用户组/角色的工作代码,运行良好。

  1. 如何获得所有组/角色?
  2. 并将用户添加到组/角色
Container  = “ou=containername,ou=xx,ou=xx,O=xxxxx”
Domain = “mydomain.com” 
group = ou=groups,ou=containername,ou=xx,ou=xx,O=xxxx

List<string> roles = new List<string>();

SearchRequest request = new SearchRequest("", "(&(objectClass=person)(mail=myusername))", System.DirectoryServices.Protocols.SearchScope.Subtree);
SearchResponse response = (SearchResponse)con.SendRequest(request);

if (response.Entries.Count == 0)
{
    return null;
}
else
{
    foreach (SearchResultEntry entry in response.Entries)
    {
        if (entry.Attributes["member"] != null)
        {
            roles = (entry.Attributes["member"].GetValues(typeof(string))).ToArray().Select(r => r.ToString()
                    .Substring(r.ToString().IndexOf("cn=") + 3,
                     r.ToString().IndexOf(",") - 3))
                    .ToList();
        }
    }
}
return roles;

【问题讨论】:

  • 您使用的是 Active Directory 还是其他 LDAP 提供程序?
  • 我正在使用 LDAP("LDAP://")。连接已经建立,因此我能够获得链接到特定用户的角色/组。
  • 对,但是服务器是什么?活动目录、OpenLDAP 等?我问是因为 AD 与其他 LDAP 服务器相比有其自身的怪癖。
  • 这是一个私有 LDAP(公司 LDAP)xxxxxxxx.com:636 我不确定我是否已经回答了你的问题。
  • 否 :) LDAP 只是一种协议——一种通信的“语言”。我想知道服务器上有什么软件。但没关系。我会尽量回答。

标签: c# ldap ldap-query


【解决方案1】:

我认为您没有使用 Active Directory。告诉我的是,您正在从用户的 member 属性中获取数据。这不是它与 Active Directory 一起使用的方式(应该是 memberOf)。

我不完全确定您要的是什么。您的标题提到了“嵌套组”,这意味着当一个组是另一个组的成员时。所以我假设这意味着你想找到用户所属的每个组以及这些组所属的所有组,等等。如果是这种情况,你真的必须找出你是什么类型的服务器在任何人给你一个好的答案之前连接到。

但是在您的问题中,您说“我怎样才能获得所有组/角色?”那么这是否意味着您只想找到存在的每个组?为此,您只需进行新搜索并将其用作过滤器:

(objectClass=group)

对于将用户添加到组,我认为应该是这样的(其中userDn 是您要添加的用户的distinguishedNamegroupDn 是组的):

var mod = new DirectoryAttributeModification {
    Name = "member",
    Operation = DirectoryAttributeOperation.Add
}
mod.Add(userDn);

var response = (ModifyResponse) connectionObject.SendRequest(
    new ModifyRequest {
        DistinguishedName = groupDn,
        Modifications = { mod }
    }
);

但我从未真正使用过LdapConnection,因此您可能需要对其进行调整。

【讨论】:

  • 我也在使用 LDAP 连接。我添加了 add 函数,但现在收到此错误“可分辨名称包含无效语法”。同样为了获得所有组,我创建了新的搜索,但没有返回。请告知 //SearchRequest sendRequest = new SearchRequest("", "(&(objectClass=group))", System.DirectoryServices.Protocols.SearchScope.Subtree); //SearchResponse sendResponse = (SearchResponse)con.SendRequest(sendRequest);
  • “可分辨名称包含无效语法。”表示您为它(为用户或组)提供的可分辨名称之一与目录中的名称不匹配。
  • 搜索群组可以试试(objectCategory=group)。这实际上取决于 LDAP 服务器的设置方式。
  • 谢谢,现在看来正朝着正确的方向前进。问题出在组名上。现在收到此错误“发生对象类冲突”。关于_response函数
  • 听起来像是一个 OpenLDAP 服务器。我在那里没有太多经验。我进行了快速搜索,可能是因为设置了错误的属性? member 可能不是正确的属性。可能是memberUid。如果没有,您必须询问服务器管理员在组上使用哪个属性作为成员资格。
【解决方案2】:

默认情况下,不检索 ADLDS 或 AD MemberOf(用户对象)成员(组对象)属性。

用户解决方案示例

SearchRequest request = new SearchRequest("", "(&(objectClass=user)(mail=myusername))", System.DirectoryServices.Protocols.SearchScope.Subtree);

request.Attributes.Add("memberOf");

或组

SearchRequest request = new SearchRequest("", "(&(objectClass=group))", System.DirectoryServices.Protocols.SearchScope.Subtree);

request.Attributes.Add("member");

Default LDAP Filters and Attributes for Users, Groups and Containers

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-14
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    相关资源
    最近更新 更多