RealmDeployDocs
GuidesIdentity providers

Connect Active Directory to Keycloak (LDAP)

Federate Microsoft Active Directory users and groups into your Keycloak realm over LDAPS, read-only, with password checks against your domain controllers.

With LDAP user federation, users sign in on the Keycloak login page with their Active Directory username and password. Keycloak looks the user up in your directory, checks the password against it, and can copy users and their AD groups into the realm. This is not identity brokering: there is no redirect to another login page, Keycloak talks to your domain controllers directly.

Checked against the Keycloak 26.8 docs and admin console, and Microsoft's Active Directory search filter docs, on 25 November 2026.

Replace auth.example.com with your RealmDeploy hostname and acme with your realm. The examples use the domain corp.example.com (DC=corp,DC=example,DC=com) and a domain controller published as ldap.corp.example.com.

Keycloak connects out to your directory, not the other way round. Your tenant runs in our cloud, so your domain controller must be reachable from the internet on a host name and port, or over a connection we agree with you. Outgoing connections from your tenant are closed by default, and we must open this one before anything below works.

Use LDAPS (ldaps://, usually port 636) only. Never expose plain LDAP (port 389) to the internet: binds would send your service account's password and every user's password in clear text.

Before you start, tell us through How to reach us how your directory is reachable: the host name, the port, and any firewall rule on your side. We check what is possible and open the connection.

Before you start

You need:

  • A service account in AD with read access to the users and groups Keycloak should see, for example CN=svc-keycloak,OU=Service Accounts,DC=corp,DC=example,DC=com. It does not need write rights, because this guide sets Keycloak to read-only.
  • The DNs of the organizational units that hold the users and the groups, for example OU=Staff,DC=corp,DC=example,DC=com and OU=Groups,DC=corp,DC=example,DC=com.
  • An LDAPS certificate on the domain controller whose name matches the host name in the connection URL. Keycloak checks the host name strictly on LDAP connections. A certificate from a public certificate authority is trusted as is. If it comes from your own internal CA, Keycloak has to trust that CA, which is a server setting we manage: send us the CA certificate (only the public certificate, never a private key) and we check it.

In Keycloak

Add the LDAP provider

Open User federation and choose Add Ldap providers (or Add new provider → LDAP if the realm already has one). Fill in the sections below, then choose Save.

Decide Edit mode and Username LDAP attribute before the first save: Keycloak creates its default mappers from them and does not change those mappers if you change the settings later.

General options

SettingValue
UI display nameactive-directory
VendorActive Directory

Choosing Active Directory fills in AD defaults for the attributes below.

Connection and authentication settings

SettingValue
Connection URLldaps://ldap.corp.example.com:636. For failover, list several domain controllers separated by spaces only.
Enable StartTLSOff (the connection is already TLS)
Use Truststore SPIAlways (the default; Keycloak marks this setting as deprecated)
Connection poolingOn (the default)
Bind typesimple
Bind DNCN=svc-keycloak,OU=Service Accounts,DC=corp,DC=example,DC=com
Bind credentialsThe service account's password

Choose Test connection, then Test authentication. The first checks that Keycloak reaches the server over TLS; the second checks the bind DN and password.

LDAP searching and updating

SettingValue
Edit modeREAD_ONLY
Users DNOU=Staff,DC=corp,DC=example,DC=com
Username LDAP attributesAMAccountName (users sign in as jdoe) or userPrincipalName (users sign in with the full name that ends in corp.example.com). The AD default is cn, which is the full name in most domains.
RDN LDAP attributecn
UUID LDAP attributeobjectGUID
User object classesperson, organizationalPerson, user
User LDAP filterOptional, for example (memberOf=CN=Keycloak Users,OU=Groups,DC=corp,DC=example,DC=com) to let in only members of one group. The filter must start with ( and end with ).
Search scopeSubtree, if users sit in OUs below the Users DN; One Level otherwise
PaginationOn

The LDAP filter builder helps you write and check the User LDAP filter.

With READ_ONLY, Keycloak never writes to AD. Users cannot change their name, email or password in Keycloak; they change their password the way they do today, in Windows or your own tools.

Synchronization settings

SettingValue
Import usersOn. Keycloak keeps a local copy of each user (never the password).
Sync RegistrationsOff
Periodic full syncOn, for example every 86400 seconds (daily)
Periodic changed users syncOn, for example every 3600 seconds (hourly)
Remove invalid users during searchesOff, so a user who disappears from AD is kept in Keycloak as read-only and disabled rather than deleted

Kerberos integration

SettingValue
Allow Kerberos authenticationOff
Use Kerberos for password authenticationOff

Kerberos (Windows single sign-on) needs a keytab file on the Keycloak server itself, and changes how browsers talk to Keycloak; this guide leaves it off.

Advanced settings

SettingValue
Trust EmailOn if your IT manages the mail attribute, so Keycloak treats those addresses as verified

After saving, open the provider's Action menu and choose Sync all users once. The periodic syncs keep it current from then on.

The default mappers

On the provider's Mappers tab, Keycloak has created mappers for username, first name, last name, email and the account dates. For Active Directory it also adds MSAD account controls, which reads userAccountControl and pwdLastSet, so a user whose AD account is disabled is disabled in Keycloak too.

Groups and roles

Add a mapper that copies AD groups into Keycloak groups:

  1. On the provider's Mappers tab, choose Add mapper.
  2. Set Name to groups and Mapper type to group-ldap-mapper, fill in the rest and choose Save:
FieldValue
LDAP Groups DNOU=Groups,DC=corp,DC=example,DC=com
Group Name LDAP Attributecn
Group Object Classesgroup
Preserve Group InheritanceOff. Keycloak gives a group one parent; AD groups can sit in several, and Keycloak's sync can fail on that.
Ignore Missing GroupsOff
Membership LDAP Attributemember
Membership Attribute TypeDN
LDAP FilterOptional, for example (cn=app-*) to copy only some groups
ModeREAD_ONLY
User Groups Retrieve StrategyGET_GROUPS_FROM_USER_MEMBEROF_ATTRIBUTE
Member-Of LDAP AttributememberOf
Drop non-existing groups during syncOff, until you have checked the result once
Groups Path/ (top level), or an existing Keycloak group such as /ad
  1. Open the mapper again and choose Sync LDAP groups to Keycloak from its action menu.

With GET_GROUPS_FROM_USER_MEMBEROF_ATTRIBUTE, Keycloak reads the user's memberOf values and keeps only the groups under LDAP Groups DN. To include groups a user belongs to through other groups, choose LOAD_GROUPS_BY_MEMBER_ATTRIBUTE_RECURSIVELY instead; Keycloak then asks AD with its in-chain matching rule (LDAP_MATCHING_RULE_IN_CHAIN), which costs the domain controller more work.

To turn AD groups into Keycloak roles instead of groups, use the role-ldap-mapper the same way, or give Keycloak groups their roles under Groups → (group) → Role mapping.

Your apps see the groups in Keycloak's tokens once their client has a Group Membership mapper (Client scopes → <client-id>-dedicated → Configure a new mapper).

Test it

  1. Under Users, search for an AD user. The user shows the federation link to active-directory.
  2. On the user's Groups tab, check the AD groups.
  3. In a private browser window, open https://auth.example.com/realms/acme/account and sign in with the AD username and password.
  4. Disable a test user in AD, wait for the next changed users sync or choose Sync changed users, and check that the sign-in is refused.

Common errors

ErrorCauseFix
Test connection fails or times outThe outgoing connection is not open yet, the host or port is wrong, or a firewall on your side drops the traffic.Tell us the host and port through How to reach us, and check your firewall.
Test connection fails with a certificate or TLS errorThe certificate's name does not match the host name in Connection URL, or it is issued by an internal CA Keycloak does not trust.Use the name on the certificate in the URL. Send us your CA certificate. The X.509 decoder shows a certificate's names.
Test authentication failsWrong Bind DN or password, or the service account is locked or expired.Copy the account's full DN from AD and reset its password.
Sync finds no usersUsers DN is wrong, Search scope is One Level while users are in sub-OUs, or the User LDAP filter matches nobody.Fix the DN, set Subtree, test the filter.
Invalid username or password. for a user who existsThe user types a different name than Username LDAP attribute holds, or the user is outside the Users DN or filter.Choose sAMAccountName or userPrincipalName to match what users type, and check the filter.
A user cannot change their passwordExpected with READ_ONLY.Users change passwords in AD.
Groups are missingThe groups are outside LDAP Groups DN, the group sync has not run, or nested membership needs the recursive strategy.Fix the DN, run Sync LDAP groups to Keycloak, or switch the strategy.
Group sync failsPreserve Group Inheritance is on and a group has several parents.Turn it off.

FAQ

Do passwords leave our directory?

Keycloak never imports passwords. Each password check is a bind from your tenant to your domain controller over LDAPS, so the directory must be reachable whenever users sign in.

Tell us how your directory is reachable today, and we check what is possible for your tenant. Until we have agreed something else with you in writing, plan for LDAPS over the internet.

Is there an option that needs no connection into our network?

If your Active Directory already syncs to Microsoft Entra ID, connect Entra ID instead: see Connect Microsoft Entra ID over OpenID Connect. Keycloak then only talks to Microsoft's cloud, not to your domain controllers.

Can Keycloak create users in AD?

Only with Edit mode WRITABLE and Sync Registrations on, and a service account with write rights. This guide keeps Keycloak read-only, which is the safer start.

Sources

On this page