Created
April 10, 2017 19:47
-
-
Save vijayjt/18385c691bf32de7d3a33a12a60af605 to your computer and use it in GitHub Desktop.
Code for testing LDAP connection from an Azure Web App in an ASE to a AD DC in a VNet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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