Skip to content

TokenCredentialCache: Adds a fallback mechanism to AAD scope override.#5337

Closed
aavasthy wants to merge 1 commit intomasterfrom
users/aavasthy/AADScope_fallback
Closed

TokenCredentialCache: Adds a fallback mechanism to AAD scope override.#5337
aavasthy wants to merge 1 commit intomasterfrom
users/aavasthy/AADScope_fallback

Conversation

@aavasthy
Copy link
Copy Markdown
Contributor

@aavasthy aavasthy commented Aug 6, 2025

Pull Request Template

Description

It's a follow up for this PR #5252, which introduces the ability to override AAD scope value. As part of the current PR, the change provides a fallback mechanism where in if the overridden scope fails then it will make another attempt with default scope.

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)

Closing issues

To automatically close an issue: closes #IssueNumber


if (!cachedAccessToken.HasValue)
{
throw new ArgumentNullException("TokenCredential.GetTokenAsync returned a null token.");
Copy link
Copy Markdown
Member

@kirankumarkolli kirankumarkolli Aug 15, 2025

Choose a reason for hiding this comment

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

Include Scopes in all exception message.

DefaultTrace.TraceError($"TokenCredential.GetTokenAsync failed with override scope '{this.overrideScope}': {ex.Message}. Retrying with default scope '{this.defaultScope}'.");

TokenRequestContext defaultContext = new TokenRequestContext(new string[] { this.defaultScope });
this.cachedAccessToken = await this.GetAndValidateTokenAsync(defaultContext);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Orrides are final right?

}
catch (Exception ex)
{
DefaultTrace.TraceError($"TokenCredential.GetTokenAsync failed with override scope '{this.overrideScope}': {ex.Message}. Retrying with default scope '{this.defaultScope}'.");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Wrapping it with our exception types gives us flexibility to include extra context like scopes.
With side-affect of mis-interpretting it as Cosmos issue. Thoughs?

/cc: @FabianMeiswinkel

{
DefaultTrace.TraceError($"TokenCredential.GetTokenAsync failed with override scope '{this.overrideScope}': {ex.Message}. Retrying with default scope '{this.defaultScope}'.");

TokenRequestContext defaultContext = new TokenRequestContext(new string[] { this.defaultScope });
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you please share what's the behavior if multiple scopes are specified?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ex: What happens if both account and generic ones are included.

Copy link
Copy Markdown
Member

@kirankumarkolli kirankumarkolli left a comment

Choose a reason for hiding this comment

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

Please check comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants