Search code examples
yii2yii-extensionsyii2-advanced-appyii2-basic-appyii2-model

Ldap login in yii2


I want to perform LDAP login in yii2.

How to do this?

This is my function in LoginForm.php.

public function authenticate($attribute, $params) {
            if (!$this->hasErrors()) {


           // var_dump($_POST); die;

            define('DOMAIN_FQDN', 'abc.com');
            define('LDAP_SERVER', '192.*.*.*');



            $ldap = ldap_connect(DOMAIN_FQDN);

           // echo  $_POST['LoginForm']['username']; 

            if ($bind = ldap_bind($ldap, $_POST['LoginForm']['username'], $_POST['LoginForm']['password'])) {
                    echo "success";
                    die;
            }
            else{
                    echo "error";
                    die;
            } 

Solution

  • I'd recommend to look at existing solutions before implementing your own.

    See this library - Adldap2.

    Here is an example of authentification:

    try {
    
        if ($provider->auth()->attempt($username, $password)) {
            // Credentials were correct.
        } else {
            // Credentials were incorrect.
        }
    
    } catch (\Adldap\Exceptions\Auth\UsernameRequiredException $e) {
        // The user didn't supply a username.
    } catch (\Adldap\Exceptions\Auth\PasswordRequiredException $e) {
        // The user didn't supply a password.
    }
    

    More info can be found in README in official docs. Info about configuring is available here.

    As for you latest error, the message is clear - check if credentials that you are provided are correct.