我正在尝试使用php-ldap连接LDAP.我使用ldap_bind()遇到了一个问题:
$username = 'josue.ruiz';
$password = 'pass';
$ldapconfig['host'] = '10.10.10.11';
$ldapconfig['port'] = 389;
$ldapconfig['basedn'] = 'dc=domain,dc=com';
$ds=ldap_connect($ldapconfig['host'], $ldapconfig['port']);
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
$dn="cn=".$username.",ou=Technology,".$ldapconfig['basedn'];
if ($bind=ldap_bind($ds, $dn, $password)) {
echo("Login correct");
} else {
echo("Login incorrect");
}
我收到这条消息:
Warning: ldap_bind(): Unable to bind to server: Invalid credentials in …
但是当我这样尝试时:
ldap_bind($ds,'josue.ruiz@domain.com','pass');
它工作正常,但对我来说它不起作用,因为我想按OU过滤,而这样我就不能.有没有人对这个问题有任何建议?
解决方法:
当您尝试执行ldap_bind时,您只是连接并确定凭据是否有效.您需要做的是将您的域添加到用户名并让它连接.然后,如果你想确定用户是否是带有ldap_search(‘)的’Technology’OU,请考虑这样做:
$domain = 'mydomain.com';
$username = 'josue.ruiz';
$password = 'pass';
$ldapconfig['host'] = '10.10.10.11';
$ldapconfig['port'] = 389;
$ldapconfig['basedn'] = 'dc=domain,dc=com';
$ds=ldap_connect($ldapconfig['host'], $ldapconfig['port']);
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
$dn="ou=Technology,".$ldapconfig['basedn'];
$bind=ldap_bind($ds, $username .'@' .$domain, $password);
$isITuser = ldap_search($bind,$dn,'(&(objectClass=User)(sAMAccountName=' . $username. '))');
if ($isITuser) {
echo("Login correct");
} else {
echo("Login incorrect");
}