ldap_sasl_bind函数的示例?

问题描述 投票:0回答:1

我徒劳地搜索了如何实现OpenLDAP函数ldap_sasl_bind

正在开发的应用程序要求我使用OpenLDAP访问Active Directory。我必须绑定到AD,我看过的每个示例程序都使用ldap_simple_bind或类似的函数,这些函数已被弃用,因此我无法使用它们。

ldap_sasl_bind函数有一个用户凭据参数,我必须提供用户名和密码进行身份验证。问题是cred参数接受指向struct berval的指针,我不知道如何实现。

microsoft docs中berval的定义如下:

    typedef struct berval {
    ULONG bv_len;
    PCHAR bv_val;
    } LDAP_BERVAL, *PLDAP_BERVAL, BERVAL, *PBERVAL, BerValue; 

这是OpenLDAP中的ldap_sasl_bind函数定义:

    int ldap_sasl_bind(LDAP *ld, const char *dn, const char 
   *mechanism, struct berval *cred, LDAPControl *sctrls[],
    LDAPControl *cctrls[], int *msgidp);

我只需访问bv_val并为其分配值即可提供用户名,我可以在bv_len中提供用户名的长度。但是我该如何提供密码?

active-directory ldap openldap
1个回答
1
投票

BerValue bv_val应该是指向密码字符串的二进制表示的指针,因为函数ldap_sasl_bind使用传入的绑定dn作为第二个参数,所以您不需要在此berval中提供用户名。

类似下面的东西应该工作(C ++):

berval cred;
cred.bv_val = (char*)password.c_str();
cred.bv_len = password.length();

res = ldap_sasl_bind(ld, dn, LDAP_SASL_SIMPLE, cred, sctrls, cctrls, msgidp);

常数LDAP_SASL_SIMPLE相当于''NULL

© www.soinside.com 2019 - 2024. All rights reserved.