Skip to content

Instantly share code, notes, and snippets.

@Ademking
Created July 23, 2020 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ademking/baf2858bb8115ebb5584868a35548e8b to your computer and use it in GitHub Desktop.
Save Ademking/baf2858bb8115ebb5584868a35548e8b to your computer and use it in GitHub Desktop.
LDAP + Windows server 2019 + Laravel

Testing LDAP Connection (Native PHP)

  1. Install:
  • Windows server 2019
  • Make windows server IP static: In Virtualbox, Machine -> Settings -> Network -> Bridged Adapter
  1. We will need these information:
  • Server IP address: (you can get it using "ipconfig" command)
  • Admin username: (Example: Administrator)
  • Admin password: (Example: 123456789A@)
  • LDAP rdn: you can use this command in Windows server
C:\> dsquery user -samid Administrator

"cn=Administrator,cn=Users,dc=server,dc=local"
  1. Test connection using this PHP code
<?php
// using ldap bind
$ldaprdn = 'cn=Administrator,cn=Users,dc=server,dc=local'; // dn
$ldappass = '123456789A@'; // user password
$ldaphost = "192.168.1.20"

// connect to ldap server
$ldapconn = ldap_connect($ldaphost) or die("Could not connect to LDAP server.");

// Set some ldap options for talking to
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);

if ($ldapconn) {

    // binding to ldap server
    $ldapbind = @ldap_bind($ldapconn, $ldaprdn, $ldappass);

    // verify binding
    if ($ldapbind) {
        //echo "LDAP bind successful...\n";
        echo "Success";
        
    } else {
        //echo "LDAP bind failed...\n";
        echo "Error";
    }
}

Note from https://www.php.net/manual/en/ldap.installation.php:

In order for this extension to work, there are DLL files that must be available to the Windows system PATH. For information on how to do this, see the FAQ entitled "How do I add my PHP directory to the PATH on Windows". Although copying DLL files from the PHP folder into the Windows system directory also works (because the system directory is by default in the system's PATH), this is not recommended. This extension requires the following files to be in the PATH: libeay32.dll and ssleay32.dll, or, as of OpenSSL 1.1 libcrypto-.dll and libssl-.dll

Laravel + LDAP

  1. Install Adldap2-Laravel
  • composer require adldap2/adldap2-laravel
  1. Run:
  • php artisan vendor:publish --provider="Adldap\Laravel\AdldapServiceProvider"
  1. in ".env":
LDAP_LOGGING=true
LDAP_HOSTS=192.168.1.20 //Server Address
LDAP_BASE_DN=dc=server,dc=local //domain
LDAP_TIMEOUT=30
LDAP_USERNAME=Administrator@server.local //admin username + @ domain
LDAP_PASSWORD=123456789A@ // admin password
  1. Create a function in any Controller to test connection:
public function test_ad()
    {
        try {
            $test_user = 'test@server.local';
            $test_pass = '123456789A@';
            Adldap::connect('default', $test_user, $test_pass);
            return "Login/pass is correct";
            // Great, we're connected!
        } catch (Adldap\Auth\BindException $e) {
            // Failed to connect.
            return "Incorrect Login/pass";
        }

    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment