Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the email instead of the username in LDAP authentication with PHP?



Currently, I am able to login using the username and password. I wish to be able to login using email address instead of the username. How do you suggest I do it?

Script:

$ldap['username'] = "domain\user123";
$ldap['password'] = "password123";
$ldap['host']   = 'site.domain.com';
$ldap['port']   = 389;
$ldap['dn'] = "CN=Users, DC=domain, DC=com";

$ldap['conn'] = ldap_connect( $ldap['host'], $ldap['port'] )
or die("Could not connect to {$ldap['host']}" );

$ldap['bind'] = ldap_bind($ldap['conn'], $ldap['username'], $ldap['password']);

if( !$ldap['bind'] )
{
echo "Failed";
}

else if( $ldap['bind'] )
{
echo "Success";
}

Any help would be very much appreciated.

Thanks so much!

like image 920
Mary Avatar asked Sep 15 '25 15:09

Mary


1 Answers

You will have to bind with a user with read-access to the LDAP-server, search for the DN of the user with the email-address in question and then use that DN to do a second bind. If the second bind is successful the user is logged in, if the second bind fails, the login fails.

I've created a gist showing that process at https://gist.github.com/heiglandreas/5689592

like image 199
heiglandreas Avatar answered Sep 17 '25 06:09

heiglandreas