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

C++ LDAP Checking if a user is a member of a specific group

本文关键字:成员 检查 用户 是否是 C++LDAP      更新时间:2023-10-16

到目前为止,我已经尝试了一段时间,但没有成功,所以希望有人能帮上忙(我也不远了!(。我只想通过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);

这不会返回任何内容,所以我尝试了更多的搜索,我发现的唯一类似的东西是-LDAPFinding Members of a group PHP,但它在PHP中,到目前为止我似乎无法将其转移到C++。

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

您的过滤器应该类似于:

(&(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是一个扩展匹配。