Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

alinad's avatar

can you help me with ldaprecord ? I'm trying to implements an LDAP authentication in an application for my company. I'm using Laravel 5.8 and the LdapRecord package

I have succeed to connect the application with the LDAP server test see Successfully connected and my to authenticate with openldap to online server its also Successfully. but when i want to do the authentication with active directory to my app it not working , Why who can help me? Here is my code :

The .env



	LDAP_LOGGING=true
	LDAP_CONNECTION=default
	LDAP_HOST=server
	LDAP_PORT=389
	LDAP_BASE_DN="dc=mydomain,dc=local"
	LDAP_TIMEOUT=5
	LDAP_SSL=false
	LDAP_TLS=false

The ldap.php


return [

	 'default' => env('LDAP_CONNECTION', 'default'),


		'connections' => [

    		'default' => [
        	'hosts' => [env('LDAP_HOST', 'server')],
        	'username' => env('LDAP_USERNAME', ''),
        	'password' => env('LDAP_PASSWORD', ''),
        	'port' => env('LDAP_PORT', 389),
        	'base_dn' => env('LDAP_BASE_DN', 'dc=mydomain,dc=local'),
        	'timeout' => env('LDAP_TIMEOUT', 5),
        	'use_ssl' => env('LDAP_SSL', false),
        	'use_tls' => env('LDAP_TLS', false),
    		],	

		],

		'logging' => env('LDAP_LOGGING', true),



			'cache' => [
    			'enabled' => env('LDAP_CACHE', false),
    			'driver' => env('CACHE_DRIVER', 'file'),
],
];

The auth.php.


return [

'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'ldap',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
        'hash' => false,
    ],
],



'providers' => [
    'users' => [
        'driver' => 'eloquent' ,
        'model' => App \ User :: class,
    ],
    'ldap' => [
        'driver' => 'ldap',
      'model' => LdapRecord\Models\ActiveDirectory\User::class,
     //   'model' => LdapRecord\Models\OpenLDAP\User::class,
        'database' => [
            'model' => App\User::class,
            'sync_passwords' => false,
            'sync_attributes' => [
                'name' => 'cn',
                'email' => 'mail',
            ],
        ],
    ]

    // 'users' => [
    //     'driver' => 'database',
    //     'table' => 'users',
    // ],
],



'passwords' => [
    'users' => [
        'provider' => 'users',
        'table' => 'password_resets',
        'expire' => 60,
        'throttle' => 60,
    ],
],



'password_timeout' => 10800,
];

The user.php model

namespace App; use
 Illuminate\Contracts\Auth\MustVerifyEmail; 
use Illuminate\Foundation\Auth\User as Authenticatable; 
use Illuminate\Notifications\Notifiable; 
use LdapRecord\Laravel\Auth\LdapAuthenticatable; 
use LdapRecord\Laravel\Auth\AuthenticatesWithLdap;

class User extends Authenticatable implements LdapAuthenticatable 
{ use Notifiable, AuthenticatesWithLdap, HasLdapUser ;

protected $fillable = [
    'name',
    'email',
    'password',

];

protected $hidden = [
        'password', 'remember_token' ,
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

The LoginController


namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
    class LoginController extends Controller
	{



	use AuthenticatesUsers;

	protected $redirectTo = RouteServiceProvider::HOME;


public function __construct()
{
    $this->middleware('guest')->except('logout');
}

protected function credentials(Request $request)
{
    return [
        'uid' => $request->get('username'),
        'password' => $request->get('password'),
    ];
}

public function username()
{
    return 'username';
}
 
 

0 likes
7 replies
Sinnbeck's avatar

Did you forget username and password in your env? LDAP_USERNAME and LDAP_PASSWORD

Create a new user just for this. It is required

alinad's avatar
	LDAP_LOGGING=true
	LDAP_CONNECTION=default
	LDAP_HOST=ldap.forumsys.com
	LDAP_USERNAME="cn=test test, ou=people dc=moliya,dc=local"
	LDAP_PASSWORD=password 
	LDAP_PORT=389
	LDAP_BASE_DN="dc=example,dc=com"
	LDAP_TIMEOUT=5
	LDAP_SSL=false
	LDAP_TLS=false
Sinnbeck's avatar

Yep. It won't work without it

Mine follows this format to work (replace with actual username)

LDAP_USERNAME="[email protected]"
1 like
alinad's avatar

thank you very much , now when i tested it shows me Successfully connected

but when i wont to auth with my app it pops up an error

These credentials do not match our records ?

alinad's avatar

Sinnbeck thank you very much, I am very grateful for your support .

Please or to participate in this conversation.