-
Notifications
You must be signed in to change notification settings - Fork 236
Add Claims and Capability Support to Managed Identity Flows #3350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# Microsoft.Identity.Web – Continuous Access Evaluation (CAE) for Managed Identity | ||
|
||
## Why Continuous Access Evaluation? | ||
|
||
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.). | ||
|
||
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. | ||
|
||
## What this spec adds to **Microsoft.Identity.Web** | ||
|
||
* **Declarative opt-in** – one configuration knob (`ClientCapabilities: [ "cp1" ]`). | ||
* **Transparent 401 recovery** – when a downstream Microsoft API responds with a 401+claims challenge, Id.Web automatically: | ||
1. extracts the claims body; | ||
2. bypasses its token cache; | ||
3. requests a fresh token that satisfies the claims; | ||
4. retries the HTTP call **once**. | ||
|
||
The goal is **zero-touch** for most developers. | ||
|
||
## Typical Flow (Managed Identity → Downstream API) | ||
|
||
```text | ||
1. Id.Web → MSI endpoint : GET /token?resource=...&xms_cc=cp1 ──▶ | ||
2. MSI → ESTS : request includes cp1 ──▶ | ||
3. ESTS → Id.Web : access_token (xms_cc claim present) ◀── | ||
4. Id.Web → Downstream API : GET /resource ⟶ 200 OK │ | ||
5. Policy change occurs │ | ||
6. Id.Web → Downstream API : GET /resource ⟶ 401 + claims payload │ | ||
7. Id.Web handles challenge (steps 1-4 again, bypassing msal cache) ──▶ | ||
``` | ||
|
||
## Design Goals | ||
|
||
| # | Goal | Success Metric | | ||
|-----|--------------------------------------------------------------|----------------------------------------------------------| | ||
| G1 | Transparent CAE retry with cache-bypass on 401 claims challenge. | Downstream API call recovers without developer code. | | ||
| G2 | Declarative client capabilities via configuration. | Single place to add `cp1`; all MI calls include it. | | ||
|
||
## Public API Impact | ||
|
||
no changes to the public api. | ||
|
||
## Configuration Example | ||
|
||
``` | ||
{ | ||
"AzureAd": { | ||
"ClientCapabilities": [ "cp1" ] | ||
}, | ||
|
||
// Example downstream API definition (Contoso Storage API) | ||
"ContosoStorage": { | ||
"BaseUrl": "https://storage.contoso.com/", | ||
"RelativePath": "data/records?api-version=1.0", | ||
"RequestAppToken": true, | ||
"Scopes": [ "https://storage.contoso.com/.default" ], | ||
"AcquireTokenOptions": { | ||
"ManagedIdentity": { | ||
// optional – omit for system-assigned MI | ||
"UserAssignedClientId": "<client-id>" | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
> **Note** : The same configuration block works in *appsettings.json* or can be supplied programmatically. | ||
|
||
|
||
## Code Snippets | ||
|
||
### Registering & Calling a Downstream API | ||
|
||
```csharp | ||
// 1 – set up the TokenAcquirerFactory (test-helper shown for brevity) | ||
var factory = TokenAcquirerFactory.GetDefaultInstance(); | ||
|
||
// 2 – register the downstream API using section "ContosoStorage" | ||
factory.Services.AddDownstreamApi("ContosoStorage", | ||
factory.Configuration.GetSection("ContosoStorage")); | ||
|
||
IServiceProvider sp = factory.Build(); | ||
IDownstreamApi api = sp.GetRequiredService<IDownstreamApi>(); | ||
|
||
// 3 – call the API (Id.Web handles CAE automatically) | ||
HttpResponseMessage resp = await api.CallApiForAppAsync("ContosoStorage"); | ||
``` | ||
|
||
### Using **IAuthorizationHeaderProvider** (advanced) | ||
|
||
`IAuthorizationHeaderProvider` is fully supported with Managed Identity. Claims challenges propagate the same way: | ||
|
||
```csharp | ||
var headerProvider = sp.GetRequiredService<IAuthorizationHeaderProvider>(); | ||
string header = await headerProvider.CreateAuthorizationHeaderForAppAsync( | ||
scope: "https://storage.contoso.com/.default", | ||
options: new AuthorizationHeaderProviderOptions | ||
{ | ||
AcquireTokenOptions = new AcquireTokenOptions | ||
{ | ||
ManagedIdentity = new ManagedIdentityOptions(), // system-assigned MI | ||
Claims = claimsChallengeJson // when retrying after 401 | ||
} | ||
}); | ||
``` | ||
|
||
## Telemetry | ||
|
||
We rely on server side telemetry for the token revocation features. | ||
|
||
Server dashboards add MI success‑rate with/without cp1. | ||
|
||
## Options as seen in MSAL | ||
|
||
 | ||
|
||
### 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) |
15 changes: 15 additions & 0 deletions
15
src/Microsoft.Identity.Web.TokenAcquisition/IManagedIdentityTestHttpClientFactory.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Microsoft.Identity.Client; | ||
|
||
namespace Microsoft.Identity.Web | ||
{ | ||
/// <summary> | ||
/// **TEST-ONLY.** Allows unit tests to supply a custom <see cref="IMsalHttpClientFactory"/>. | ||
/// </summary> | ||
internal interface IManagedIdentityTestHttpClientFactory | ||
{ | ||
IMsalHttpClientFactory Create(); | ||
} | ||
} |
4 changes: 3 additions & 1 deletion
4
src/Microsoft.Identity.Web.TokenAcquisition/PublicAPI/net462/InternalAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
const Microsoft.Identity.Web.IDWebErrorMessage.ExceptionAcquiringTokenForConfidentialClient = "IDW10501: Exception acquiring token for a confidential client: " -> string! | ||
const Microsoft.Identity.Web.IDWebErrorMessage.ExceptionAcquiringTokenForConfidentialClient = "IDW10501: Exception acquiring token for a confidential client: " -> string! | ||
Microsoft.Identity.Web.IManagedIdentityTestHttpClientFactory | ||
Microsoft.Identity.Web.IManagedIdentityTestHttpClientFactory.Create() -> Microsoft.Identity.Client.IMsalHttpClientFactory! |
4 changes: 3 additions & 1 deletion
4
src/Microsoft.Identity.Web.TokenAcquisition/PublicAPI/net472/InternalAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
const Microsoft.Identity.Web.IDWebErrorMessage.ExceptionAcquiringTokenForConfidentialClient = "IDW10501: Exception acquiring token for a confidential client: " -> string! | ||
const Microsoft.Identity.Web.IDWebErrorMessage.ExceptionAcquiringTokenForConfidentialClient = "IDW10501: Exception acquiring token for a confidential client: " -> string! | ||
Microsoft.Identity.Web.IManagedIdentityTestHttpClientFactory | ||
Microsoft.Identity.Web.IManagedIdentityTestHttpClientFactory.Create() -> Microsoft.Identity.Client.IMsalHttpClientFactory! |
4 changes: 3 additions & 1 deletion
4
src/Microsoft.Identity.Web.TokenAcquisition/PublicAPI/net6.0/InternalAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
const Microsoft.Identity.Web.IDWebErrorMessage.ExceptionAcquiringTokenForConfidentialClient = "IDW10501: Exception acquiring token for a confidential client: " -> string! | ||
Microsoft.Identity.Web.ClientInfoJsonContext | ||
Microsoft.Identity.Web.ClientInfoJsonContext | ||
Microsoft.Identity.Web.IManagedIdentityTestHttpClientFactory | ||
Microsoft.Identity.Web.IManagedIdentityTestHttpClientFactory.Create() -> Microsoft.Identity.Client.IMsalHttpClientFactory! |
4 changes: 3 additions & 1 deletion
4
src/Microsoft.Identity.Web.TokenAcquisition/PublicAPI/net7.0/InternalAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
const Microsoft.Identity.Web.IDWebErrorMessage.ExceptionAcquiringTokenForConfidentialClient = "IDW10501: Exception acquiring token for a confidential client: " -> string! | ||
Microsoft.Identity.Web.ClientInfoJsonContext | ||
Microsoft.Identity.Web.ClientInfoJsonContext | ||
Microsoft.Identity.Web.IManagedIdentityTestHttpClientFactory | ||
Microsoft.Identity.Web.IManagedIdentityTestHttpClientFactory.Create() -> Microsoft.Identity.Client.IMsalHttpClientFactory! |
4 changes: 3 additions & 1 deletion
4
src/Microsoft.Identity.Web.TokenAcquisition/PublicAPI/net8.0/InternalAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
const Microsoft.Identity.Web.IDWebErrorMessage.ExceptionAcquiringTokenForConfidentialClient = "IDW10501: Exception acquiring token for a confidential client: " -> string! | ||
Microsoft.Identity.Web.ClientInfoJsonContext | ||
Microsoft.Identity.Web.ClientInfoJsonContext | ||
Microsoft.Identity.Web.IManagedIdentityTestHttpClientFactory | ||
Microsoft.Identity.Web.IManagedIdentityTestHttpClientFactory.Create() -> Microsoft.Identity.Client.IMsalHttpClientFactory! |
4 changes: 3 additions & 1 deletion
4
src/Microsoft.Identity.Web.TokenAcquisition/PublicAPI/net9.0/InternalAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
const Microsoft.Identity.Web.IDWebErrorMessage.ExceptionAcquiringTokenForConfidentialClient = "IDW10501: Exception acquiring token for a confidential client: " -> string! | ||
Microsoft.Identity.Web.ClientInfoJsonContext | ||
Microsoft.Identity.Web.ClientInfoJsonContext | ||
Microsoft.Identity.Web.IManagedIdentityTestHttpClientFactory | ||
Microsoft.Identity.Web.IManagedIdentityTestHttpClientFactory.Create() -> Microsoft.Identity.Client.IMsalHttpClientFactory! |
4 changes: 3 additions & 1 deletion
4
...icrosoft.Identity.Web.TokenAcquisition/PublicAPI/netstandard2.0/InternalAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
const Microsoft.Identity.Web.IDWebErrorMessage.ExceptionAcquiringTokenForConfidentialClient = "IDW10501: Exception acquiring token for a confidential client: " -> string! | ||
const Microsoft.Identity.Web.IDWebErrorMessage.ExceptionAcquiringTokenForConfidentialClient = "IDW10501: Exception acquiring token for a confidential client: " -> string! | ||
Microsoft.Identity.Web.IManagedIdentityTestHttpClientFactory | ||
Microsoft.Identity.Web.IManagedIdentityTestHttpClientFactory.Create() -> Microsoft.Identity.Client.IMsalHttpClientFactory! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Identity.Abstractions; | ||
using Microsoft.Identity.Web; | ||
using System.Net; | ||
using System.Text.Json; | ||
|
||
// ── 1. bootstrap factory (reads appsettings.json automatically) ───────── | ||
var factory = TokenAcquirerFactory.GetDefaultInstance(); | ||
|
||
// optional console logging | ||
factory.Services.AddLogging(b => b.AddConsole().SetMinimumLevel(LogLevel.Warning)); | ||
|
||
// ── 2. register the downstream API using the "AzureKeyVault" section ──── | ||
factory.Services.AddDownstreamApi("AzureKeyVault", | ||
factory.Configuration.GetSection("AzureKeyVault")); | ||
IServiceProvider sp = factory.Build(); | ||
IDownstreamApi api = sp.GetRequiredService<IDownstreamApi>(); | ||
|
||
// ── 3. call the vault (app-token path) ────────────────────────────────── | ||
HttpResponseMessage response = await api.CallApiForAppAsync("AzureKeyVault"); | ||
|
||
if (response.StatusCode != HttpStatusCode.OK) | ||
{ | ||
Console.WriteLine($"Vault returned {(int)response.StatusCode} {response.ReasonPhrase}"); | ||
return; | ||
} | ||
|
||
//Get the secret value from the response | ||
using var doc = JsonDocument.Parse(await response.Content.ReadAsStringAsync()); | ||
|
||
// Check if the "value" property exists and is a string | ||
if (doc.RootElement.TryGetProperty("value", out var valueElement) && valueElement.ValueKind == JsonValueKind.String) | ||
{ | ||
// Retrieve the secret value but do not print it | ||
string secret = valueElement.GetString()!; | ||
|
||
// Optionally, you can check if the secret is not null or empty | ||
if (!string.IsNullOrEmpty(secret)) | ||
{ | ||
Console.WriteLine("Secret retrieved successfully (non-null)."); | ||
} | ||
else | ||
{ | ||
Console.WriteLine("Secret value was empty."); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
// authentication settings (apply to the whole app) | ||
"AzureAd": { | ||
// Continuous Access Evaluation capability at app-level | ||
"ClientCapabilities": [ "cp1" ] | ||
}, | ||
|
||
// downstream API settings (per-resource) | ||
"AzureKeyVault": { | ||
"BaseUrl": "https://msidlabs.vault.azure.net/", | ||
"RelativePath": "secrets/msidlab4?api-version=7.4", | ||
"RequestAppToken": true, | ||
"Scopes": [ "https://vault.azure.net/.default" ], | ||
// per request settings | ||
"AcquireTokenOptions": { | ||
"ManagedIdentity": { | ||
// user-assigned MI; omit for system-assigned | ||
"UserAssignedClientId": "4b7a4b0b-ecb2-409e-879a-1e21a15ddaf6" | ||
} | ||
} | ||
}, | ||
|
||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Warning", | ||
"Microsoft": "Information" | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.