Authentication

LDAP Authentication: How It Works and Best Practices

Browser window with a password field, cloud and identity icons

What Is LDAP Authentication? 

LDAP authentication checks a user's username and password against a directory server, such as OpenLDAP or Microsoft Active Directory, by sending a "bind" request over the LDAP protocol. It's common for internal apps, VPNs and infrastructure inside a company network. For web and SaaS apps that need single sign-on, SAML or OpenID Connect is usually the better fit.

LDAP (Lightweight Directory Access Protocol) is a widely-used open directory services protocol, which allows computer systems to access user directory information over a network. LDAP provides a way to organize information (often user authentication information) in a hierarchical manner and to access this information quickly.

LDAP authentication is a process of verifying the identity of a user by checking the provided credentials (username and password) against the data stored in an LDAP directory server. The directory server holds information about all authorized users in the system and their attributes such as passwords, names, and access privileges. 

When a user tries to log in, the system sends the user’s credentials to the directory server and the server validates the information. If the information matches what is stored in the directory, the user is granted access, otherwise the authentication request is denied.

In this article:

Why Is LDAP Authentication Important? 

The following are some of the key benefits of using LDAP for authentication:

  • Centralized management: With LDAP, user authentication information is stored in a centralized location, making it easier to manage and update.
  • Scalability: LDAP is designed to handle large volumes of user authentication data, making it an ideal solution for large organizations with many users.
  • Interoperability: LDAP is a standard protocol and is supported by many different platforms, making it easy to integrate with other systems and applications.
  • Security: LDAP traffic can be encrypted with LDAPS (port 636) or StartTLS. Without one of them, a simple bind sends the password in clear text, so always require encryption.
  • Efficiency: LDAP is designed to be fast and efficient, making it well-suited for real-time authentication requests.

LDAP vs. Active Directory: What Are the Differences?

Active Directory is a directory service created by Microsoft for use in Windows-based networks. It provides centralized management of resources, including users, computers, and other network devices, and is designed to make administration and management of large, complex enterprise networks easier.

LDAP and Active Directory are both directory services used for managing and organizing information, but they have some key differences, including:

The comparison is a little uneven, because LDAP is a protocol and Active Directory is a directory product that supports LDAP. Applications often use LDAP to talk to Active Directory.

LDAPActive Directory
PurposeOpen, vendor-neutral directory protocolMicrosoft-specific directory service for Windows-based networks
FunctionalityMainly focused on directory services, often used for authenticationComprehensive directory service including authentication, authorization, etc.
ScalabilityBasic scalability featuresAdvanced scalability features, such as multiple domain controllers and replication
IntegrationCan be integrated with a wide range of technologiesTightly integrated with other Microsoft technologies, such as Windows Server
SecuritySupports encryption through LDAPS or StartTLSSupports advanced security features, such as fine-grained access control
Ease of AdministrationBasic administration toolsAdvanced administration tools and a graphical user interface

To summarize, LDAP is a basic directory protocol that is often used for authentication, while Active Directory is a comprehensive directory service that is well-suited for large, complex enterprise networks. The choice between the two will depend on the specific requirements of the organization. LDAP is also often compared with SAML: LDAP lets applications look up and verify users against a directory, usually inside a corporate network, while SAML passes signed authentication assertions from an identity provider to web applications for single sign-on. Many organizations use both together; see our guide to SAML vs LDAP.

How Does LDAP Authentication Work? 

LDAP authentication typically works as follows:

  1. The user provides their credentials (username and password) to the system.
  2. The system sends a bind request to the LDAP server, containing the user’s credentials.
  3. The LDAP server checks the user’s credentials against the data stored in its directory.
  4. If the credentials match, the server sends a success message to the system, indicating that the user has been authenticated.
  5. The system grants the user access to the requested resource.
  6. If the credentials do not match, the server sends a failure message to the system, indicating that the user has not been authenticated.
  7. The system denies the user access to the requested resource.

Use LDAPS or StartTLS so the credentials in the bind request are encrypted between the application and the directory server, and verify the server's certificate. Many directories are deployed with replicas for availability, so applications can keep authenticating users if one server is down.

LDAP vs SAML and SSO: when to use which

LDAP and SAML both deal with user identity, but they solve different problems. LDAP lets an application check a password against a directory. SAML, and its newer counterpart OpenID Connect (OIDC), let an identity provider sign a user in and send the application a signed assertion or token, which is how single sign-on (SSO) works on the web.

LDAP SAML / OIDC (SSO)
What it does Looks up users and checks passwords in a directory Passes a signed sign-in result from an identity provider to the app
Who sees the password The application receives it and forwards it to the directory Only the identity provider; the app never sees it
Where it works best Inside a company network: Linux and Windows servers, VPNs, Wi-Fi, network devices, legacy apps Web and SaaS apps reached over the internet
Single sign-on No. Users type their password into each app Yes. One sign-in at the identity provider covers many apps
MFA Not part of the protocol Enforced by the identity provider
Typical use in B2B SaaS Rare. Customers rarely expose their directory to an outside app Standard way to let customers sign in with Okta, Entra ID or Google Workspace

Use LDAP when an internal system, such as a server, network device or older on-premises app, needs to check credentials against your own directory. Use SAML or OIDC when you build a web or SaaS app, especially one that sells to companies: their IT teams expect to connect their identity provider, keep MFA and passwords under their control, and never hand your app their users' passwords. Many organizations run both: Active Directory as the directory, with an identity provider such as Microsoft Entra ID, ADFS or Okta in front of it for SSO.

For a deeper comparison, see SAML vs LDAP and our guide to single sign-on.

LDAP Authentication Code Examples 

Simple Authentication

The code below uses the python-ldap library to connect to an OpenLDAP-style directory over LDAPS and verify the user’s credentials. If the bind is successful, the user’s credentials are correct, and the function returns True. If the bind fails due to invalid credentials, the function returns False. If something else goes wrong, an error message is printed, and the function returns False.

The code looks like this:

import ldap

def authenticate(username, password):
    if not password:
        return False  # some servers accept an empty password as an anonymous bind

    ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_DEMAND)
    server = "ldaps://ldap.example.com:636"
    base_dn = "dc=example,dc=com"
    user_dn = "uid={},{}".format(ldap.dn.escape_dn_chars(username), base_dn)

    conn = ldap.initialize(server)
    conn.protocol_version = ldap.VERSION3
    try:
        conn.simple_bind_s(user_dn, password)
        return True  # the bind succeeded, so the credentials are correct
    except ldap.INVALID_CREDENTIALS:
        return False  # wrong username or password
    except ldap.LDAPError as error:
        print("Error:", error)
        return False
    finally:
        conn.unbind_s()

Authentication with Two Organizational Units (OUs)

The code below is similar to the previous example, with one key difference: the user_dn is specified with two organizational units (OUs), ou=users and ou=intranet. This makes it possible to have separate user directories for different parts of the organization. 

The ou=intranet OU ensures that only users within the intranet can authenticate, while the ou=users OU narrows it down to the specific user directory. The rest of the code remains the same, performing the LDAP bind and checking the user’s credentials.

Here’s the same function with the user’s entry under two organizational units:

import ldap

def authenticate(username, password):
    if not password:
        return False

    ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_DEMAND)
    server = "ldaps://ldap.example.com:636"
    base_dn = "dc=example,dc=com"
    user_dn = "uid={},ou=users,ou=intranet,{}".format(
        ldap.dn.escape_dn_chars(username), base_dn
    )

    conn = ldap.initialize(server)
    conn.protocol_version = ldap.VERSION3
    try:
        conn.simple_bind_s(user_dn, password)
        return True
    except ldap.INVALID_CREDENTIALS:
        return False
    except ldap.LDAPError as error:
        print("Error:", error)
        return False
    finally:
        conn.unbind_s()

With Active Directory, you usually bind with the user principal name instead of a DN, for example conn.simple_bind_s("jane@example.com", password). In both cases, reject empty passwords: some directories treat a bind with an empty password as an anonymous bind and report success, even though RFC 4513 says servers should refuse it.

Authentication and Authorization with Frontegg

LDAP is built for checking passwords inside a company network. A B2B SaaS app needs something different: each customer company signs in through its own identity provider, and your app still has to decide what each user can do in each customer account.

Frontegg handles both. Customers connect their identity provider over SAML or OIDC, including providers that sit in front of Active Directory, and provision users with SCIM. Frontegg then gives you organizations, roles and permissions, and an admin portal where each customer's admins manage their own users. Frontegg is free up to 7,500 monthly active users, including 5 SSO/SCIM connections.

Start For Free