OIDC SPA
GitHubHome
v6
  • Documentation
  • Release Notes & Upgrade Instructions
v6
  • Installation
  • Basic Usage
  • Web API
  • Auto Login
  • Auto Logout
  • Error Management
  • Mock
  • User Account Management
  • User Session Initialization
  • Tokens Renewal
  • Setup Guides
    • React Router
    • TanStack Router
    • Full-Stack with Node REST API
  • Providers Configuration
    • Keycloak
    • Auth0
    • Microsoft Entra ID
    • Google OAuth 2.0
    • Other OIDC Provider
  • Resources
    • Why No Client Secret?
    • End of third-party cookies
    • JWT Of the Access Token
    • Discord Server
  • User Impersonation
  • Sponsors
Powered by GitBook
On this page
  • Configuring Entra ID to Issue a JWT Access Token
  • Steps to Configure a Custom Scope
  • Validating the Token on the Backend
  • Registering Your Application
  • Configuring oidc-spa
  • Testing the Setup

Was this helpful?

Export as PDF
  1. Providers Configuration

Microsoft Entra ID

Formerly Azure Active Directory

PreviousAuth0NextGoogle OAuth 2.0

Last updated 2 months ago

Was this helpful?

Configuring Entra ID to Issue a JWT Access Token

By default, Entra ID issues opaque Access Tokens, which can only be validated by your backend via the Microsoft Graph API.

Steps to Configure a Custom Scope

  1. In the left panel, select "Microsoft Entra ID".

  2. Navigate to "Manage > App Registrations".

  3. Click "New Registration".

  4. Enter "My App - API" as the name, then click Register.

  5. Set Supported Account Type to Accounts in this organization.

  6. In the left menu, go to "Manage > Expose API".

  7. Click "Add a scope".

  8. Configure as follows, then click "Add Scope":

    • Application ID URI: api://my-app-api (then save and continue)

    • Scope name: access_as_user

    • Who can consent: Admins and Users

    • Admin Consent Display Name: "JWT Access Token"

    • Admin Consent Description: "Ensure issuance of a JWT Access Token"

    • User Consent Display Name: "View your basic profile"

    • User Consent Description: "Allows the app to see your basic profile (e.g., name, picture, user name, email address)"

    • State: Enabled

Validating the Token on the Backend


Registering Your Application

  1. In the left panel, select "Microsoft Entra ID".

  2. Navigate to "Manage > App Registrations".

  3. Click "New Registration".

  4. Enter "My App" as the display name (replace with your actual app name).

  5. Set Supported Account Type to Accounts in this organization.

  6. Click Register.

  7. Click "Add a Redirect URI".

  8. Click "Add Platform" > "Single-Page Application".

  9. Set Redirect URIs:

    • Production: https://my-app.com/ (include trailing slash; adjust if hosted under a subpath, e.g., https://my-app.com/dashboard/)

    • Local Development: http://localhost:5173/ (include trailing slash; adjust based on your dev server)

  10. Ensure "Access Token" and "ID Token" are checked.

  11. Click Save.

  12. In the left panel, go to "API Permissions".

  13. Click "Add a permission".

  14. Click "APIs My Organization Uses".

  15. Select "My App - API".

  16. Check "access_as_user", then click "Add permission".

  17. In the left panel, click "Overview" and copy:

    • Application (client) ID

    • Directory (tenant) ID

These are required to configure oidc-spa.


Configuring oidc-spa

import { createOidc } from "oidc-spa";

// Directory (tenant) ID:
const directoryId = "XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
// Application (client) ID:
const clientId = "XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
// Application ID URI: (Of the API!)
const applicationIdUri_api= "api://my-app-api/access_as_user";

export const prOidc = createOidc({
    issuerUri: `https://login.microsoftonline.com/${directoryId}/v2.0`,
    clientId,
    scopes: ["profile", applicationIdUri_api],
    homeUrl: import.meta.env.BASE_URL
});
import { createReactOidc } from "oidc-spa/react";

// Directory (tenant) ID:
const directoryId = "XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
// Application (client) ID:
const clientId = "XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
// Application ID URI: (Of the API!)
const applicationIdUri_api= "api://my-app-api/access_as_user";

export const { OidcProvider, useOidc, getOidc } = createReactOidc({
    issuerUri: `https://login.microsoftonline.com/${directoryId}/v2.0`,
    clientId,
    scopes: ["profile", applicationIdUri_api],
    homeUrl: import.meta.env.BASE_URL
});

Testing the Setup

To test your configuration:

npx degit https://github.com/keycloakify/oidc-spa/examples/tanstack-router-file-based oidc-spa-tanstack-router
cd oidc-spa-tanstack-router
cp .env.local.sample .env.local

# Uncomment the Microsoft Entra ID section and comment out the Keycloak section.
# Update the values with your own.

yarn
yarn dev

To enable validation of access tokens in a non-vendor-locked way—such as demonstrated in —you need to configure a custom scope.

Go to .

To validate the token on the backend, ensure that the aud claim in the JWT access token matches api://my-app-api. For more details, refer to the .

Go to .

the Web API section
Microsoft Azure Portal
Web API documentation
Microsoft Azure Portal