我当前正在尝试使用Windows API(Winldap.h)链接到LDAP服务器。当我使用Linux标头(ldap.h)时,绑定正常工作,接下来我可以进行研究。在Windows下,无法绑定,我总是出现错误“(81)Server out of service”。但是参数是一样的!
这是Linux下的代码:
#include <ldap.h>
LDAP* ldapHandler;
ldap_initialize( &ldapHandler, "ldap://IP_SERVER");
int version( LDAP_VERSION3 );
ldap_set_option(ldapHandler, LDAP_OPT_PROTOCOL_VERSION, &version);
berval* serverCreds;
berval mycreds{};
mycreds.bv_val = "myPwd";
mycreds.bv_len = sizeof("myPwd");
int res = ldap_sasl_bind_s(ldapHandler, "cn=myuser,cn=users,dc=domain,dc=com", LDAP_SASL_AUTOMATIC, &mycreds, nullptr, nullptr, &serverCreds);
if(res != LDAP_SUCCESS) {
std::cout << "Bind fail with: " << ldap_err2string(res) << std::endl;
} else {
std::cout << "Bind success !" << std::endl;
}
显示绑定成功。现在在Win32下:
#include <winldap.h>
LDAP* ldapHandler;
ldapHandler = ldap_init("ldap://IP_SERVER", LDAP_PORT);
int version(LDAP_VERSION3);
ldap_set_option(ldapHandler, LDAP_OPT_PROTOCOL_VERSION, &version);
berval* serverCreds;
berval mycreds{};
mycreds.bv_val = "myPwd";
mycreds.bv_len = sizeof("myPwd");
int res = ldap_sasl_bind_s(ldapHandler, "cn=myuser,cn=users,dc=domain,dc=com", "DIGEST-MD5", &mycreds, nullptr, nullptr, &serverCreds);
if (res != LDAP_SUCCESS) {
std::cout << "Bind fail with: " << ldap_err2string(res) << std::endl;
}
else {
std::cout << "Bind success !" << std::endl;
}
有人可以帮我吗,因为标识符和服务器在这两个代码中是完全相同的...唯一可以发挥的区别是身份验证方法,在Linux下,宏LDAP_SASL_AUTOMATIC
独自处理。在Windows上,我指定"DIGEST-MD5"
。但是我的LDAP服务器处理得很好:
ldapsearch -h IP_SERVER -p 389 -x -b "" -s base -LLL supportedSASLMechanisms
supportedSASLMechanisms: GSSAPI
supportedSASLMechanisms: GSS-SPNEGO
supportedSASLMechanisms: EXTERNAL
supportedSASLMechanisms: DIGEST-MD5
谢谢