Skip to content

Add WithReservedScopes and WithCachePartitionKey public APIs#6014

Merged
bgavrilMS merged 7 commits into
mainfrom
iinglese/cache-partition-key
May 19, 2026
Merged

Add WithReservedScopes and WithCachePartitionKey public APIs#6014
bgavrilMS merged 7 commits into
mainfrom
iinglese/cache-partition-key

Conversation

@iNinja

@iNinja iNinja commented May 18, 2026

Copy link
Copy Markdown
Contributor

Summary

Two new public APIs for transfer token scenarios and cache isolation:

WithReservedScopes(bool offlineAccessScope)

Extension method on AcquireTokenByAuthorizationCodeParameterBuilder. When offlineAccessScope is set to false, MSAL omits offline_access from the token request while continuing to send openid and profile. The removal happens in TokenClient after the regular scope building, without affecting scope construction.

Only available on auth code redemption flows. Unreachable by client credentials, silent, OBO, or interactive flows.

WithCachePartitionKey(string key, string value)

Extension method on BaseAbstractAcquireTokenParameterBuilder<T> in the Extensibility namespace. Adds a client-side discriminator to the AT cache key without sending anything to the token endpoint. Enables parallel token sessions for the same user without cache interference.

Validates inputs: ArgumentNullException for null, ArgumentException for empty key.

Motivation

These APIs support the transfer token flow for 1P mobile app-to-browser scenarios (PBI 3562385). A web service redeems a short-lived MSA transfer token alongside an existing long-lived OIDC session for the same user. Without these APIs:

  • MSAL always adds offline_access, causing MSA to return an unwanted refresh token
  • Same-user AT cache entries collide, causing session downgrade

Builds on PR #5993

The symmetric AT filter and partition-aware write-side eviction merged in PR #5993. This PR adds the two public APIs on top.

Changes

File Change
Extensibility/AcquireTokenParameterBuilderExtensions.cs WithReservedScopes + WithCachePartitionKey extension methods
AcquireTokenCommonParameters.cs SendOfflineAccessScope flag
AuthenticationRequestParameters.cs Thread SendOfflineAccessScope
TokenClient.cs Strip offline_access after default scope build
PublicAPI.Unshipped.txt (x6 TFMs) API declarations
WithReservedScopesTests.cs 3 tests
WithCachePartitionKeyTests.cs 4 tests

Testing

  • 7 unit tests covering both APIs
  • Validated against live MSA endpoints (transfer token redemption without offline_access confirmed: no RT returned)
  • Validated cache partition isolation with mock IdP PoC

Copilot AI review requested due to automatic review settings May 18, 2026 15:43
@iNinja iNinja requested a review from a team as a code owner May 18, 2026 15:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds two new public builder APIs on BaseAbstractAcquireTokenParameterBuilder<T> to (1) optionally prevent MSAL from auto-injecting reserved OIDC scopes and (2) allow callers to add a purely client-side access-token cache partition discriminator (not sent to the token endpoint). This extends existing internal patterns (overridden scopes + additional cache key components) to enable transfer-token scenarios and cache isolation.

Changes:

  • Introduces WithExactScopes(bool exactScopes = true) to bypass reserved-scope injection for /token requests.
  • Introduces WithCachePartitionKey(string key, string value) to add a cache-key-only discriminator via CacheKeyComponents.
  • Threads ExactScopes through request parameters and adds unit tests for both APIs; updates PublicAPI analyzer baselines across TFMs.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/Microsoft.Identity.Test.Unit/PublicApiTests/WithExactScopesTests.cs Adds unit tests validating reserved-scope injection is suppressed/preserved depending on WithExactScopes.
tests/Microsoft.Identity.Test.Unit/PublicApiTests/WithCachePartitionKeyTests.cs Adds unit tests validating cache key components are populated and not sent to the token endpoint.
src/client/Microsoft.Identity.Client/PublicApi/netstandard2.0/PublicAPI.Unshipped.txt Declares the new public APIs for netstandard2.0.
src/client/Microsoft.Identity.Client/PublicApi/net8.0/PublicAPI.Unshipped.txt Declares the new public APIs for net8.0.
src/client/Microsoft.Identity.Client/PublicApi/net8.0-ios/PublicAPI.Unshipped.txt Declares the new public APIs for net8.0-ios.
src/client/Microsoft.Identity.Client/PublicApi/net8.0-android/PublicAPI.Unshipped.txt Declares the new public APIs for net8.0-android.
src/client/Microsoft.Identity.Client/PublicApi/net472/PublicAPI.Unshipped.txt Declares the new public APIs for net472.
src/client/Microsoft.Identity.Client/PublicApi/net462/PublicAPI.Unshipped.txt Declares the new public APIs for net462.
src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs Uses ExactScopes to override default scope handling for token requests.
src/client/Microsoft.Identity.Client/Internal/Requests/AuthenticationRequestParameters.cs Threads ExactScopes from common parameters into request parameters.
src/client/Microsoft.Identity.Client/ApiConfig/Parameters/AcquireTokenCommonParameters.cs Adds ExactScopes flag to common parameters.
src/client/Microsoft.Identity.Client/ApiConfig/BaseAbstractAcquireTokenParameterBuilder.cs Adds WithExactScopes and WithCachePartitionKey public APIs and plumbs them into common parameters.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@iNinja iNinja force-pushed the iinglese/cache-partition-key branch from 54fc586 to a59d7fd Compare May 18, 2026 16:43

@4gust 4gust left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs Outdated
Address review feedback: scope override logic now lives in
ConfidentialAuthCodeRequest.GetOverriddenScopes() rather than
RequestBase, ensuring it only affects auth code redemption flows.
Other flows (client credentials, OBO, interactive, etc.) are unaffected.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 19, 2026 09:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 14 out of 14 changed files in this pull request and generated 5 comments.

iNinja and others added 2 commits May 19, 2026 10:38
…e build

Move scope removal from ConfidentialAuthCodeRequest.GetOverriddenScopes
override to TokenClient.SendTokenRequestAsync. The regular scope building
runs as normal (GetDefaultScopes adds all reserved scopes), then
offline_access is stripped from the result. No override, no scope
construction, no changes to existing method signatures.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Use ArgumentNullException for null, ArgumentException for empty string
per .NET guidelines.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@iNinja iNinja changed the title Add WithExactScopes and WithCachePartitionKey public APIs Add WithReservedScopes and WithCachePartitionKey public APIs May 19, 2026
Copilot AI review requested due to automatic review settings May 19, 2026 10:29
WithReservedScopes now targets AcquireTokenByAuthorizationCodeParameterBuilder
instead of BaseAbstractAcquireTokenParameterBuilder. This prevents it from
appearing on flows where suppressing offline_access would be incorrect
(client credentials, silent, OBO, interactive).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 4 comments.

Comment thread src/client/Microsoft.Identity.Client/OAuth2/TokenClient.cs Outdated
Add WithInstanceDiscovery(false) to all test builders to prevent
shared instance discovery cache from affecting mock handler ordering.
Use AcquireTokenByAuthorizationCode consistently in partition key tests.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 19, 2026 11:20
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.

@bgavrilMS bgavrilMS merged commit 022dcde into main May 19, 2026
19 checks passed
@bgavrilMS bgavrilMS deleted the iinglese/cache-partition-key branch May 19, 2026 12:31
Robbie-Microsoft added a commit that referenced this pull request May 22, 2026
These APIs were added in PR #6014 (022dcde) but were accidentally absent
from this branch due to shallow clone grafting. Restoring to fix CI build
failures in WithReservedScopesTests.cs and WithCachePartitionKeyTests.cs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Robbie-Microsoft added a commit that referenced this pull request May 22, 2026
…om PR #6014

Restores AcquireTokenCommonParameters.SendOfflineAccessScope and all 6
PublicAPI.Unshipped.txt entries for WithCachePartitionKey and WithReservedScopes
that were lost due to a prior merge conflict resolution.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Robbie-Microsoft added a commit that referenced this pull request May 22, 2026
…RequestParameters

Restores the offline_access scope filtering logic in TokenClient.SendTokenRequestAsync
and the SendOfflineAccessScope property on AuthenticationRequestParameters, both
originally added in PR #6014 but lost in a prior merge conflict resolution.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants