With the PHP function ldap_connect
it's working:
My controller :
/**
* @Route("/ldap", name="ldap")
*/
public function ldap(Request $request) {
$ldaprdn = 'cn=read-only-admin,dc=example,dc=com';
$ldappass = 'password';
$ldapconn = ldap_connect("ldap://ldap.forumsys.com")
or die("Impossible de se connecter au serveur LDAP.");
if ($ldapconn) {
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
// Connexion au serveur LDAP
$ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass);
var_dump($ldapbind);
// Vérification de l'authentification
if ($ldapbind) {
echo "Connexion LDAP OK ...";
} else {
echo "Connexion LDAP KO ...";
}
}
}
Result : bool(true)
But if I use Symfony Ldap it's don't work, did you know why ?
My controller :
use Symfony\Component\Ldap\Ldap;
[...]
/**
* @Route("/ldap", name="ldap")
*/
public function ldap(Request $request) {
$host = 'ldap.forumsys.com';
$port = 389;
$version = 3;
$dn = 'cn=read-only-admin,dc=example,dc=com';
$pwd = 'password';
$ldap = Ldap::create('ext_ldap', [
'host' => $host,
'port' => $port,
'version' => $version,
]);
$ldap->bind($dn, $pwd);
var_dump($ldap);
}
Result :
["connection":"Symfony\Component\Ldap\Adapter\ExtLdap\Connection":private]=> resource(31) of type (ldap link) ["config":protected]=> array(8) { ["host"]=> string(17) "ldap.forumsys.com" ["version"]=> int(3) ["encryption"]=> string(4) "none" ["port"]=> int(389) ["connection_string"]=> string(28) "ldap://ldap.forumsys.com:389" ["debug"]=> bool(false) ["referrals"]=> bool(false) ["options"]=> array(3) { ["network_timeout"]=> string(2) "60" ["protocol_version"]=> int(3) ["referrals"]=> bool(false) } } } ["entryManager":"Symfony\Component\Ldap\Adapter\ExtLdap\Adapter":private]=> NULL } }
I use an online Ldap test server
CodePudding user response:
It actually works, it seems like you don't really know how to use it.
Please, read: The Ldap Component
While ldap_bind
returns false when authentication was unsuccessful, Symfony Ldap component's bind
method returns nothing on success and throws an exception on error (which you can see from the source code). In your case, you must check if the connection/authentication was successful with a try/catch block:
<?php
use Symfony\Component\Ldap\Exception\ConnectionException;
try {
$ldap->bind($dn, $pwd);
} catch (ConnectionException $exception) {
// Error message based on your original code
echo 'Connexion LDAP KO:' . $exception->getMessage();
}