ldap_search_s returns LDAP_OPERATIONS_ERROR

ldap_search_s returns LDAP_OPERATIONS_ERROR

本文关键字:OPERATIONS ERROR LDAP search ldap returns      更新时间:2023-10-16

我有带有基本Active Directory的Windows Server 2016,我正在尝试通过WinApi(Wldap32,Winldap.h(使用C++登录并检查用户组。

身份验证似乎有效,但我在ldap_search_s(ldap_search_sW(后得到LDAP_OPERATIONS_ERROR

const std::wstring addressStr = L"192.168.78.3";
const std::wstring usernameStr = L"myuser"; // the same as in the filter below
const std::wstring passwordStr = L"";
ULONG version = LDAP_VERSION3;
LDAP *pLdapConnection = ldap_init(const_cast<wchar_t *>(addressStr.c_str()), static_cast<ULONG>(config_.adPort()));
if (pLdapConnection == nullptr) {
throw ...;
}
ULONG ret = ldap_set_option(pLdapConnection, LDAP_OPT_PROTOCOL_VERSION, static_cast<void *>(&version));
if (ret != LDAP_SUCCESS) {
throw ...;
}
ret = ldap_connect(pLdapConnection, nullptr);
if (ret != LDAP_SUCCESS) {
throw ...;
}
ret = ldap_bind_s(pLdapConnection, const_cast<wchar_t *>(usernameStr.c_str()), const_cast<wchar_t *>(passwordStr.c_str()),
LDAP_AUTH_SIMPLE);
if (ret != LDAP_SUCCESS) {
if (ret == 0x31) {
throw ...;
}
throw ...;
}
LDAPMessage *pSearchResult = nullptr;
std::wstring filter = L"(&(sAMAccountName=myuser)(memberof=CN=Administrators))";
std::wstring dn = L"dc=whatever,dc=net";
ret = ldap_search_s(pLdapConnection, const_cast<wchar_t *>(dn.c_str()), LDAP_SCOPE_SUBTREE, const_cast<wchar_t *>(filter.c_str()),
nullptr, 0, &pSearchResult);
// ret == 1 == LDAP_OPERATIONS_ERROR;

这些例子可能会对你有所帮助。

//Example DN
const std::wstring usernameStr = L"uid=user,ou=People,dc=company,dc=com"
//Example UPN
const std::wstring usernameStr = L"user@company.com"
//Example NT-style login
const std::wstring usernameStr = L"COMPANY\user"

这个问题实际上与身份验证有关,而不是与搜索查询有关。

我使用了空密码,ldap_bind_s没有报告任何错误,因为显然在这种情况下它是匿名绑定的(看起来 MSDN 中没有提到它(https://stackoverflow.com/a/27873735/964478。(这是一个测试虚拟机,我不记得密码,但记得早些时候我尝试为此用户设置空密码,尽管可能没有成功(

相关文章: