Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code for testing LDAP connection from an Azure Web App in an ASE to a AD DC in a VNet
<?php
$domain = 'acme.local';
$username = 'aduser';
$password = 'replaceWithPassword';
$ldapconfig['host'] = '192.168.21';
$ldapconfig['port'] = 389;
$ldapconfig['basedn'] = 'dc=acme,dc=local';
$ldap_dn = "DC=acme,DC=local";
//print_r($ldapconfig);
$ds=ldap_connect($ldapconfig['host'], $ldapconfig['port']);
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
if ($ds) {
echo("ldap connect completed\n");
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
$bind=ldap_bind($ds, $username.'@'.$domain, $password);
if ($bind) {
echo "LDAP bind successful...$bind <br />";
$attributes= array( "sn", "givenname", "mail", "samaccountname");
$filter = '(&(objectCategory=person)(samaccountname=*))';
$results = ldap_search($ds, $ldap_dn, $filter, $attributes);
if ($retval) {
echo("Login correct <br />");
$info = ldap_get_entries($ds, $results);
print_r($info);
for ($i=0; $i<$entries["count"]; $i++)
{
echo $entries[$i]["displayname"]
[0]."(".$entries[$i]["l"][0].")<br />";
}
} else {
echo("Login incorrect <br />".ldap_error($retval));
}
}else {
echo "ERROR: LDAP bind failed...<br />";
}
ldap_unbind($bind);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment