|
| 1 | +# Microsoft.Identity.Web – Continuous Access Evaluation (CAE) for Managed Identity |
| 2 | + |
| 3 | +## Why Continuous Access Evaluation? |
| 4 | + |
| 5 | +Continuous Access Evaluation (CAE) lets Microsoft Entra ID revoke tokens or demand extra claims almost immediately when risk changes (user disabled, password reset, network change, policy update, etc.). |
| 6 | + |
| 7 | +A workload opts-in by sending the client-capability **`cp1`** when acquiring tokens. Entra then includes an **`xms_cc`** claim in the token so downstream Microsoft services know the caller can handle claims challenges. |
| 8 | + |
| 9 | +## What this spec adds to **Microsoft.Identity.Web** |
| 10 | + |
| 11 | +* **Declarative opt-in** – one configuration knob (`ClientCapabilities: [ "cp1" ]`). |
| 12 | +* **Transparent 401 recovery** – when a downstream Microsoft API responds with a 401+claims challenge, Id.Web automatically: |
| 13 | + 1. extracts the claims body; |
| 14 | + 2. bypasses its token cache; |
| 15 | + 3. requests a fresh token that satisfies the claims; |
| 16 | + 4. retries the HTTP call **once**. |
| 17 | + |
| 18 | +The goal is **zero-touch** for most developers. |
| 19 | + |
| 20 | +## Typical Flow (Managed Identity → Downstream API) |
| 21 | + |
| 22 | +```text |
| 23 | +1. Id.Web → MSI endpoint : GET /token?resource=...&xms_cc=cp1 ──▶ |
| 24 | +2. MSI → ESTS : request includes cp1 ──▶ |
| 25 | +3. ESTS → Id.Web : access_token (xms_cc claim present) ◀── |
| 26 | +4. Id.Web → Downstream API : GET /resource ⟶ 200 OK │ |
| 27 | +5. Policy change occurs │ |
| 28 | +6. Id.Web → Downstream API : GET /resource ⟶ 401 + claims payload │ |
| 29 | +7. Id.Web handles challenge (steps 1-4 again, bypassing msal cache) ──▶ |
| 30 | +``` |
| 31 | + |
| 32 | +## Design Goals |
| 33 | + |
| 34 | +| # | Goal | Success Metric | |
| 35 | +|-----|--------------------------------------------------------------|----------------------------------------------------------| |
| 36 | +| G1 | Transparent CAE retry with cache-bypass on 401 claims challenge. | Downstream API call recovers without developer code. | |
| 37 | +| G2 | Declarative client capabilities via configuration. | Single place to add `cp1`; all MI calls include it. | |
| 38 | + |
| 39 | +## Public API Impact |
| 40 | + |
| 41 | +no changes to the public api. |
| 42 | + |
| 43 | +## Configuration Example |
| 44 | + |
| 45 | +``` |
| 46 | +{ |
| 47 | + "AzureAd": { |
| 48 | + "ClientCapabilities": [ "cp1" ] |
| 49 | + }, |
| 50 | +
|
| 51 | + // Example downstream API definition (Contoso Storage API) |
| 52 | + "ContosoStorage": { |
| 53 | + "BaseUrl": "https://storage.contoso.com/", |
| 54 | + "RelativePath": "data/records?api-version=1.0", |
| 55 | + "RequestAppToken": true, |
| 56 | + "Scopes": [ "https://storage.contoso.com/.default" ], |
| 57 | + "AcquireTokenOptions": { |
| 58 | + "ManagedIdentity": { |
| 59 | + // optional – omit for system-assigned MI |
| 60 | + "UserAssignedClientId": "<client-id>" |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +> **Note** : The same configuration block works in *appsettings.json* or can be supplied programmatically. |
| 68 | +
|
| 69 | + |
| 70 | +## Code Snippets |
| 71 | + |
| 72 | +### Registering & Calling a Downstream API |
| 73 | + |
| 74 | +```csharp |
| 75 | +// 1 – set up the TokenAcquirerFactory (test-helper shown for brevity) |
| 76 | +var factory = TokenAcquirerFactory.GetDefaultInstance(); |
| 77 | + |
| 78 | +// 2 – register the downstream API using section "ContosoStorage" |
| 79 | +factory.Services.AddDownstreamApi("ContosoStorage", |
| 80 | + factory.Configuration.GetSection("ContosoStorage")); |
| 81 | + |
| 82 | +IServiceProvider sp = factory.Build(); |
| 83 | +IDownstreamApi api = sp.GetRequiredService<IDownstreamApi>(); |
| 84 | + |
| 85 | +// 3 – call the API (Id.Web handles CAE automatically) |
| 86 | +HttpResponseMessage resp = await api.CallApiForAppAsync("ContosoStorage"); |
| 87 | +``` |
| 88 | + |
| 89 | +### Using **IAuthorizationHeaderProvider** (advanced) |
| 90 | + |
| 91 | +`IAuthorizationHeaderProvider` is fully supported with Managed Identity. Claims challenges propagate the same way: |
| 92 | + |
| 93 | +```csharp |
| 94 | +var headerProvider = sp.GetRequiredService<IAuthorizationHeaderProvider>(); |
| 95 | +string header = await headerProvider.CreateAuthorizationHeaderForAppAsync( |
| 96 | + scope: "https://storage.contoso.com/.default", |
| 97 | + options: new AuthorizationHeaderProviderOptions |
| 98 | + { |
| 99 | + AcquireTokenOptions = new AcquireTokenOptions |
| 100 | + { |
| 101 | + ManagedIdentity = new ManagedIdentityOptions(), // system-assigned MI |
| 102 | + Claims = claimsChallengeJson // when retrying after 401 |
| 103 | + } |
| 104 | + }); |
| 105 | +``` |
| 106 | + |
| 107 | +## Telemetry |
| 108 | + |
| 109 | +We rely on server side telemetry for the token revocation features. |
| 110 | + |
| 111 | +Server dashboards add MI success‑rate with/without cp1. |
| 112 | + |
| 113 | +## Options as seen in MSAL |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +### reference - [How to use Continuous Access Evaluation enabled APIs in your applications](https://learn.microsoft.com/en-us/entra/identity-platform/app-resilience-continuous-access-evaluation?tabs=dotnet) |
0 commit comments