C ++ LDAP检查用户是否为特定组的成员

时间:2020-07-08 15:06:25

标签: c++ ldap

到目前为止,尝试这种方法一段时间没有成功,所以希望有人可以提供帮助(而且我离我也不远!)。我只想通过LDAP返回用户是否是特定组的成员。到目前为止,我有以下代码;

int authMethod = LDAP_AUTH_SIMPLE;
LDAP* pLdapConnection = NULL;
ULONG version = LDAP_VERSION3;
ULONG getOptSuccess = 0;
ULONG connectSuccess = 0;
INT returnCode = 0;
int retSearch = 0;
LDAPMessage *res;
int num_entries = 0, num_refs = 0;

pLdapConnection = ldap_init((char*)m_Hostname.GetString(), LDAP_PORT);

returnCode = ldap_set_option(pLdapConnection,
    LDAP_OPT_PROTOCOL_VERSION,
    (void*)&version);


// Connect to the server.
connectSuccess = ldap_connect(pLdapConnection, NULL);

// Bind
returnCode = ldap_bind_s(pLdapConnection, (char*)m_Username.GetString(), (char*)m_Password.GetString(), authMethod);

// Attempt to search for user
retSearch = ldap_search_s(pLdapConnection, "dc=as,dc=local", LDAP_SCOPE_SUBTREE, "(&(sAMAccountName = examplename))", NULL, NULL, &res);

到目前为止,所有这些方法一直有效,直到搜索为止,例如-我想在“技术”组中搜索用户“用户名”。我已经尝试过以下内容;

retSearch = ldap_search_s(pLdapConnection, "dc=as,dc=local", LDAP_SCOPE_SUBTREE, "(&(sAMAccountName=username)(memberof=CN=Technical))",
    nullptr, 0, &pSearchResult);

那什么都不返回,所以我尝试搜索更多,发现的唯一类似的东西是-LDAP Finding Members of a group PHP,但是它在PHP中,到目前为止,我似乎还不能将其转移到C ++。 / p>

任何正确方向的帮助都会有所帮助,因为我无法解决。 :-)

1 个答案:

答案 0 :(得分:1)

您的过滤器应类似于:

(&(objectClass=user)(sAMAccountName=yourUserName)
  (memberOf=CN=YourGroup,OU=Users,DC=YourDomain,DC=com))

要包括由于组嵌套而产生的成员资格:

(&(objectClass=user)(sAMAccountName=yourUserName)
  (memberOf:1.2.840.113556.1.4.1941:=cn=YourGroup,ou=Users,dc=YourDomain,dc=com))

数字1.2.840.113556.1.4.1941是扩展匹配。