Connect Microsoft Entra ID to Keycloak (OIDC)
Let users sign in to your Keycloak realm with their Microsoft Entra ID work account over OpenID Connect, and map Entra groups to Keycloak groups.
Users choose Microsoft on your Keycloak login page, sign in with their Entra ID work account, and come back to your app with a Keycloak token. Keycloak is the broker: your apps keep talking only to Keycloak and do not change.
Checked against the Microsoft Entra ID docs and Keycloak 26.8 on 25 November 2026.
Replace auth.example.com with your RealmDeploy hostname and acme with your realm. <tenant-id> is your Entra Directory (tenant) ID. The guide uses entra-id as the identity provider alias, so the redirect URI is https://auth.example.com/realms/acme/broker/entra-id/endpoint.
Keycloak calls Entra from your tenant: it redeems the login code at Entra's token endpoint and downloads Entra's signing keys (JWKS) to check the ID token. Outgoing connections from your tenant are closed by default. Before you test, tell us the host login.microsoftonline.com on port 443 through How to reach us, and we open it. If you leave Disable user info off (step below), Keycloak also calls graph.microsoft.com, so name that host too.
Which Keycloak provider to use
Keycloak offers two ways to add Entra ID. Use OpenID Connect v1.0.
| OpenID Connect v1.0 (this guide) | Built-in Microsoft provider | |
|---|---|---|
| What Keycloak reads | The signed ID token, checked against the issuer https://login.microsoftonline.com/<tenant-id>/v2.0 | Your profile from Microsoft Graph (/me) with an access token; the ID token is not used |
| Tenant | Fixed by the discovery URL you enter | Multi-tenant common endpoints unless you fill in Tenant ID |
| Groups claim | Read from the ID token | Not read |
| Group and role mappers | Advanced Claim to Group and Claim to Role are available | Those two mappers are offered only for OpenID Connect providers |
With the tenant-specific discovery URL, Keycloak only accepts ID tokens issued by your tenant, and the Entra groups claim can drive Keycloak groups and roles.
In Microsoft Entra ID
Register the app
- In the Microsoft Entra admin center, go to Entra ID → App registrations and choose New registration.
- Enter a Name, for example
Keycloak acme. - Under Supported account types, choose Single tenant only.
- Choose Register. On the Overview page, copy the Application (client) ID and the Directory (tenant) ID.
Add the redirect URI
- Under Manage, open Authentication and choose Add Redirect URI.
- Choose the Web platform and enter
https://auth.example.com/realms/acme/broker/entra-id/endpoint. Choose Configure. - Add a second redirect URI to the same Web platform:
https://auth.example.com/realms/acme/broker/entra-id/endpoint/logout_response. Keycloak sends users there after it signs them out of Entra, and Entra only accepts a post-logout address that is registered as a redirect URI.
You do not need the ID tokens (used for implicit and hybrid flows) checkbox. Keycloak uses the authorization code flow.
Create a client secret
- Open Certificates & secrets → Client secrets → New client secret.
- Enter a description, choose an expiry and choose Add.
- Copy the secret's Value right away. Entra shows it only once. The Secret ID is not the secret.
Secrets last at most 24 months. Sign-in through Entra stops working the day the secret expires, so note the date. Microsoft recommends certificates over client secrets for production. This guide uses a secret, the method Keycloak's own Microsoft instructions use.
Add claims to the ID token
Under Manage, open Token configuration:
- Choose Add optional claim, pick the ID token type, select
email,given_nameandfamily_name, and choose Add. - Choose Add groups claim. Pick Groups assigned to the application (recommended) or Security groups. Leave the ID token value on Group ID and choose Add.
With Groups assigned to the application, only groups you assign to the app appear in the token. Assign them in Entra ID → Enterprise apps, open the app, then Users and groups → Add user/group. Entra drops the groups claim completely when a user is in more than 200 groups, so assigning only the groups you need keeps it working.
In Keycloak
Add the identity provider
- Open Identity providers and choose OpenID Connect v1.0 (from the list, or under Add provider if the realm already has one).
- Fill in the form and choose Add:
| Setting | Value |
|---|---|
| Alias | entra-id |
| Display name | Microsoft (the button text on the login page) |
| Use discovery endpoint | On |
| Discovery endpoint | https://login.microsoftonline.com/<tenant-id>/v2.0/.well-known/openid-configuration |
| Client authentication | Client secret sent in the request body |
| Client ID | The Application (client) ID |
| Client Secret | The secret Value |
The Redirect URI shown at the top of the form must match the one you registered in Entra character for character.
Check the imported settings
Keycloak fills the endpoints from the discovery document. On the provider's Settings tab, check these and choose Save:
| Setting | Value |
|---|---|
| Issuer | https://login.microsoftonline.com/<tenant-id>/v2.0 |
| Validate Signatures | On |
| Use JWKS URL | On |
| JWKS URL | https://login.microsoftonline.com/<tenant-id>/discovery/v2.0/keys |
| Scopes (under Advanced) | openid profile email |
| Disable user info (under Advanced) | On. Keycloak then reads everything from the ID token and does not call graph.microsoft.com. |
| Backchannel logout (under Advanced) | Off. Logout then runs through the browser and needs no extra outgoing connection. |
| Trust Email (under Advanced settings) | Off. See First login and account linking. |
| Sync mode (under Advanced settings) | Force, so names, email and group mappings update at every login |
You can open the discovery document in the OIDC discovery viewer to compare the values.
Groups and roles
Each Entra group arrives as its object ID in the groups claim. Map each one with its own mapper:
- Create the target group under Groups, for example
admins. - Open Identity providers → entra-id → Mappers and choose Add mapper.
- Fill it in and choose Save:
| Field | Value |
|---|---|
| Name | entra-admins |
| Sync mode override | Inherit |
| Mapper type | Advanced Claim to Group |
| Claims | Key groups, value the Entra group's Object ID |
| Regex Claim Values | Off |
| Group | admins |
To grant a role instead of a group, use Claim to Role with Claim groups, Claim Value the group's object ID, and the Role to grant.
With Sync mode Force, Keycloak re-checks the claim at every login and removes the user from the group when the claim no longer matches. With Import, the mapper only runs at the user's first login.
Your apps see these groups in Keycloak's own tokens once the app's client has a group mapper: on the client, open Client scopes → <client-id>-dedicated → Configure a new mapper → Group Membership.
First login and account linking
The first time someone signs in through Entra, Keycloak runs the First login flow (by default first broker login):
- Review Profile shows the user's profile when a required field is missing.
- Create User If Unique creates a new Keycloak user and links it to the Entra account, unless a Keycloak user with the same email or username already exists.
- If one exists, Keycloak shows Account already exists. The user can choose Add to existing account and must then prove they own that account: by a link sent by email (only when the realm has SMTP set up, which is another outgoing connection) or by signing in with that account's password.
After the first login, Keycloak links the account by Entra's sub claim, which does not change when the user's email or UPN changes.
Trust Email tells Keycloak to treat the email from Entra as verified. Leave it off. Microsoft states that the email claim is not guaranteed to be correct and can change, and should never be used for authorization. Keycloak's own docs call automatic linking of existing accounts a potential security hole. Do not build a first login flow that links accounts by email without asking the user to prove ownership (for example one with Automatically set existing user), unless every account in the realm comes from this one Entra tenant.
Test it
- In a private browser window, open
https://auth.example.com/realms/acme/accountand choose Microsoft. - Sign in with an Entra user. You come back to the account console, signed in.
- In the admin console, open Users, find the user, and check the Identity provider links tab (shows
entra-id) and the Groups tab (shows the mapped groups).
To send users straight to Entra without the Keycloak login page, add kc_idp_hint=entra-id to the app's login request. The identity provider redirect builder writes the URL for you. If a login fails, turn on Save events under Realm settings → Events → User events settings and look at the Events page for the error details.
Common errors
| Error | Cause | Fix |
|---|---|---|
AADSTS50011 (redirect URI does not match) | The redirect URI in Entra differs from the one Keycloak sends, often a different alias or a missing path segment. | Copy the Redirect URI from the Keycloak provider page into Entra. The redirect URI matcher shows the difference. |
AADSTS7000215 (invalid client secret) | The Secret ID was pasted instead of the Value, or the secret expired. | Create a new secret and paste its Value into Client Secret. |
AADSTS700016 (application not found in the directory) | Wrong client ID, or the discovery URL points to a different tenant. | Check the Application (client) ID and the tenant ID in the discovery URL. |
AADSTS50105 (user not assigned) | Assignment required is on for the enterprise app and the user is not assigned. | Assign the user or one of their groups under Users and groups. |
Keycloak shows Unexpected error when authenticating with identity provider after the Entra sign-in | The outgoing connection to login.microsoftonline.com is not open yet, or the secret is wrong. | Tell us the host through How to reach us. Check the secret. |
| Group mapper adds nothing | The groups claim is not in the ID token, the user is in more than 200 groups, or Sync mode is Import and this is not the first login. | Add the groups claim for the ID token, use Groups assigned to the application, set Sync mode to Force. |
| Account already exists page | A Keycloak user with the same email already exists. | Expected. The user links the accounts by proving they own the existing one. |
The OAuth error lookup explains standard OAuth error codes.
FAQ
Can I use the built-in Microsoft provider instead?
It works for basic sign-in when you fill in Tenant ID. It reads the profile from Microsoft Graph instead of the ID token, so the Entra groups claim does not reach Keycloak and the group and role mappers in this guide are not available. It also needs an outgoing connection to graph.microsoft.com.
Can guest users from other organizations sign in?
Yes, if they are guests in your tenant. A single-tenant app accepts members and guests of that tenant. Users of other tenants who are not guests in yours cannot sign in.
How do I rotate the client secret?
Create a new secret in Entra, paste its Value into Client Secret on the Keycloak provider, choose Save, test a login, then delete the old secret in Entra.
Can Keycloak skip its own login page for everyone?
Yes. In Authentication, open the browser flow, choose the gear on Identity Provider Redirector and set Default Identity Provider to entra-id.
Sources
- Keycloak Server Administration Guide: Integrating identity providers (general configuration, OpenID Connect v1.0, Microsoft, mappers, first login flow, default identity provider)
- Keycloak source 26.8.1: MicrosoftIdentityProvider and AdvancedClaimToGroupMapper
- Microsoft: Register an app in Microsoft Entra ID
- Microsoft: Add a redirect URI
- Microsoft: Add and manage app credentials
- Microsoft: Configure optional claims
- Microsoft: Optional claims reference
- Microsoft: ID token claims reference
- Microsoft: OpenID Connect on the Microsoft identity platform
Keycloak SSO for Portainer
Sign in to Portainer with Keycloak through its Custom OAuth provider, with user provisioning and, in Business Edition, teams from a groups claim.
Connect Microsoft Entra ID to Keycloak (SAML)
Broker Microsoft Entra ID logins into your Keycloak realm over SAML 2.0 with a non-gallery enterprise app, signed assertions and Entra groups mapped to Keycloak groups.