我正在用 C++ 编写一个程序,该程序连接到 Active Directory 服务器,搜索内容,然后将该内容转储到终端。我的问题是,我能够成功进行身份验证和连接的唯一方法是使用用户的显示名称(例如测试用户)。我想更改此身份验证方法以改用用户的登录名。(例如 tuser@example.com)
这是负责设置会话的代码:
char * _hostName = "The_Computer_Name";
char * _dn = "CN=Test User,DC=EXAMPLE,DC=COM";
char * _ps = "The_Password123";
int ldap_Query::session_init(){
if (_pLdapConnection != nullptr) return 0;
_pLdapConnection = ldap_init(_hostName, LDAP_PORT);
if ( _pLdapConnection == nullptr) {
int err = ldap_get_option(_pLdapConnection, LDAP_OPT_ERROR_NUMBER, &err);
throw LdapQueryException(ldap_err2string(err));
}
unsigned long lRtn = ldap_simple_bind_s(_pLdapConnection, _dn,_ps);
if (lRtn != LDAP_SUCCESS) {
ldap_unbind(_pLdapConnection);
_pLdapConnection = nullptr;
throw LdapQueryException(ldap_err2string(lRtn));
}
return 0;
}
我认为我正在寻找的是用户主体名称,但还没有看到使用它进行身份验证的示例。我试过用“CN = tuser,DC = EXAMPLE,DC = COM”替换_dn,但没有运气。
谢谢你的帮助!