我正在用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;
}
我认为我正在寻找的是User-Principal-Name,但还没有看到使用它进行身份验证的示例。我试过用“CN = tuser,DC = EXAMPLE,DC = COM”替换_dn,但是没有运气。
感谢您的帮助!
答案 0 :(得分:2)
您无法直接使用upn名称执行ldap绑定。
当您通过ldap对目录进行简单绑定时,您需要使用您绑定的用户的完全限定DN及其密码。
要以您希望的方式使用userprincipalname
,您需要使用服务帐户或匿名(如果允许)使用其提供的userprincipalname
属性搜索用户。
使用类似的搜索。
(&(objectclass=user)(userprincipalname=<supplied value>))
如果获得匹配项,则从生成的用户条目中获取DN,然后绑定用户。