Entra ID + JWT
Overview
Microsoft Entra ID (formerly Azure Active Directory / Azure AD) is Microsoft's cloud-based Identity and Access Management (IAM) platform. It provides authentication, authorization, and identity services for web apps, APIs, and cloud-native applications.
JWT (JSON Web Token) is the industry-standard token format used by Entra ID to convey identity and authorization claims between parties.
Core Concepts
| Concept | Description |
|---|---|
| Tenant | Your organization's dedicated instance of Entra ID |
| Application Registration | Register your app to obtain credentials |
| Client ID | Unique identifier for your registered app |
| Tenant ID | Your organization's directory ID |
| Client Secret / Certificate | App credentials for server-to-server flows |
| Scope | What permissions the token grants |
| Claims | Key-value pairs inside the JWT (user info, roles, etc.) |
| Access Token | Short-lived token to call APIs |
| ID Token | Token containing user identity information |
| Refresh Token | Long-lived token to obtain new access tokens |
JWT Structure
A JWT consists of three Base64URL-encoded parts separated by dots:
header.payload.signature
Header
{
"alg": "RS256",
"typ": "JWT"
}
Payload (Claims)
{
"iss": "https://login.microsoftonline.com/{tenant}/v2.0",
"sub": "abc123",
"aud": "api://your-client-id",
"exp": 1717000000,
"iat": 1716996400,
"name": "John Doe",
"preferred_username": "john@example.com",
"oid": "user-object-id",
"roles": ["Admin", "Reader"],
"scp": "User.Read"
}
Common Claims
| Claim | Description |
|---|---|
iss | Issuer — Entra ID endpoint |
sub | Subject — unique user ID (per app) |
aud | Audience — your API's app ID |
exp | Expiration time |
oid | Object ID — user's unique ID across the tenant |
name | Full name |
preferred_username | Email / UPN |
roles | App roles assigned to user |
scp | Delegated scopes |
tid | Tenant ID |
Authentication Flows
OAuth 2.0 / OIDC Flows
| Flow | Use Case |
|---|---|
| Authorization Code + PKCE | Web apps, SPAs |
| Client Credentials | Service-to-service (no user) |
| On-behalf-of (OBO) | API calling another API |
| Device Code | CLI tools, IoT devices |
App Registration in Azure Portal
- Go to Azure Portal → Microsoft Entra ID → App registrations
- Click New registration, enter a name
- Set redirect URI (e.g.,
https://localhost:5001/signin-oidc) - Copy Application (client) ID and Directory (tenant) ID
- Under Certificates & secrets, create a Client secret
- Under Expose an API, add a scope (e.g.,
api://your-client-id/access_as_user) - Under App roles, define roles (e.g.,
Admin,Reader)
Setting Up ASP.NET Core with Entra ID
Install Package
dotnet add package Microsoft.Identity.Web
appsettings.json
{
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"TenantId": "your-tenant-id",
"ClientId": "your-client-id",
"Audience": "api://your-client-id",
"CallbackPath": "/signin-oidc"
}
}