C++ 如何使用ldap\u simple\u bind\s绑定到active directory服务器(ldaps://)
我已经编写了一个示例程序来验证ldaps服务器上的用户,程序如下:C++ 如何使用ldap\u simple\u bind\s绑定到active directory服务器(ldaps://),c++,c,active-directory,openldap,C++,C,Active Directory,Openldap,我已经编写了一个示例程序来验证ldaps服务器上的用户,程序如下: #define LDAP_DEPRECATED 1 #define HOST "ldaps://10.10.10.10:636" #define BASEDN "cn=Manager,dc=example,dc=com" int main(){ LDAP *ld = NULL,*ld2=NULL; int returnCode = 0,rc=0; int reqcer
#define LDAP_DEPRECATED 1
#define HOST "ldaps://10.10.10.10:636"
#define BASEDN "cn=Manager,dc=example,dc=com"
int main(){
LDAP *ld = NULL,*ld2=NULL;
int returnCode = 0,rc=0;
int reqcert = LDAP_OPT_X_TLS_NEVER;
const int ldap_version=LDAP_VERSION3;
char *dn,*outvalue;
if(ldap_initialize(&ld, HOST))
{
perror( "ldap_initialize" );
TRACE("LDAP initialized Failed");
return( 1 );
}
TRACE("LDAP initialized Successfully");
TRACE("Going to set Protocol version to 3.\n");
returnCode = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, (void *)&ldap_version);
if (returnCode != LDAP_SUCCESS)
return 1;
TRACE("Setting Protocol version to 3");
returnCode = ldap_set_option (ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &reqcert);
if (returnCode != LDAP_SUCCESS)
{
TRACE("LDAP_OPT_X_TLS_REQUIRE_CERT failed :"<<ldap_err2string(returnCode));
TRACE("Return Code :"<<returnCode);
return 1;
}
returnCode = ldap_bind_s(ld,BASEDN,"hello123",LDAP_AUTH_SIMPLE);
if( returnCode != LDAP_SUCCESS )
{
TRACE("Authentication Failed, nessage returned is :"<<ldap_err2string(returnCode));
TRACE("Error code value returned is :"<<returnCode);
fprintf(stderr, "ldap_simple_bind_s: %s\n", ldap_err2string(returnCode) );
return( 1 );
}
TRACE("Successful authentication for admin user");
rc = ldap_search_ext_s(ld,"dc=example,dc=com", LDAP_SCOPE_SUBTREE,"uid=ldapuser1", NULL, 0, NULL, NULL, NULL, 0, &result);
if ( rc != LDAP_SUCCESS ) {
TRACE ("DN search failed, error message returned is :"<<ldap_err2string(rc));
TRACE ("Error code returned is :"<<rc);
fprintf(stderr, "ldap_search_ext_s: %s\n", ldap_err2string(rc));
}
else if (rc == LDAP_SUCCESS)
{
TRACE("DN successfully searched ");
}
for ( e = ldap_first_entry( ld, result ); e != NULL; e = ldap_next_entry( ld, e ) ) {
if ( (dn = ldap_get_dn( ld, e )) != NULL )
{
printf( "dn: %s\n", dn );
ldap_initialize(&ld2,HOST);
rc=ldap_simple_bind_s(ld2,dn,"ldapuser1");
TRACE("Result Here "<<rc);
if (rc != 0)
{
printf("Failed.\n");
} else {
printf("Works.\n");
ldap_unbind(ld2);
}
ldap_memfree( dn );
return 0;
}
}
ldap_msgfree( result );
printf("ldap entry not found \n");
#定义LDAP#已弃用1
#“定义主机”ldaps://10.10.10.10:636"
#定义BASEDN“cn=Manager,dc=example,dc=com”
int main(){
LDAP*ld=NULL,*ld2=NULL;
int returnCode=0,rc=0;
int reqcert=LDAP\u OPT\u X\u TLS\u NEVER;
const int ldap_version=ldap_VERSION3;
char*dn,*outvalue;
if(ldap_初始化(&ld,主机))
{
perror(“ldap_初始化”);
跟踪(“LDAP初始化失败”);
申报表(1);
}
跟踪(“LDAP已成功初始化”);
跟踪(“将协议版本设置为3。\n”);
returnCode=ldap\u set\u选项(ld、ldap\u OPT\u协议版本,(void*)和ldap\u版本);
if(returnCode!=LDAP\u成功)
返回1;
跟踪(“将协议版本设置为3”);
returnCode=ldap\u set\u选项(ld、ldap\u OPT\u X\u TLS\u REQUIRE\u CERT和reqcert);
if(returnCode!=LDAP\u成功)
{
TRACE(“LDAP_OPT_X_TLS_REQUIRE_CERT failed:”您的服务器设置是否为LDAP列出636?即telnet 10.10.10 636是否会导致成功连接,telnet 10.10.10 636工作正常。