Skip to content

include caeEnabled: true so token is correctly cached#3573

Open
ramsessanchez wants to merge 2 commits intomainfrom
ramsess/fix-DeviceCodeAuthFlow
Open

include caeEnabled: true so token is correctly cached#3573
ramsessanchez wants to merge 2 commits intomainfrom
ramsess/fix-DeviceCodeAuthFlow

Conversation

@ramsessanchez
Copy link
Copy Markdown
Contributor

@ramsessanchez ramsessanchez commented Mar 31, 2026

Ensure CaeEnabled is set to true when aquiring token so that the retrieved token is cached correctly. MSAL looks for tokens in separate CaeEnabled cache. Enabling this ensures subsequent calls made to the api's are able to retrieve the token prior to executing the call.
Add try catch to ensure device code uri is always shown and doesn't result in misleading Object reference not set to an instance of an object. message

fixes #3495

@ramsessanchez ramsessanchez requested a review from a team as a code owner March 31, 2026 19:29
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes -UseDeviceCode follow-on call failures by ensuring the initial token acquisition uses a CAE-enabled TokenRequestContext, matching the CAE-enabled context used during subsequent API calls so MSAL can locate the token in the correct cache. It also hardens device-code message output to avoid misleading null-reference errors when the normal output writer is unavailable.

Changes:

  • Acquire the sign-in token with isCaeEnabled: true to ensure CAE-capable tokens are cached and retrievable for later requests.
  • Wrap device-code message output in a fallback path to always display the device code URI/message.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +163 to +167
try
{
GraphSession.Instance.OutputWriter.WriteObject(code.Message);
}
catch
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

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

The catch { ... } in the device code callback catches all exceptions and then silently falls back to Console.WriteLine, which can mask unexpected failures (and may trigger analyzer warnings like CA1031). Prefer avoiding exceptions for control flow here: e.g., null-check GraphSession.Instance/OutputWriter first, or catch a narrow set of expected exceptions (like NullReferenceException/InvalidOperationException) and optionally emit a debug/warning with the original exception details before falling back to console output.

Suggested change
try
{
GraphSession.Instance.OutputWriter.WriteObject(code.Message);
}
catch
if (GraphSession.Instance != null && GraphSession.Instance.OutputWriter != null)
{
GraphSession.Instance.OutputWriter.WriteObject(code.Message);
}
else

Copilot uses AI. Check for mistakes.
Comment on lines +287 to +289
// Use isCaeEnabled: true to match the TokenRequestContext that AzureIdentityAccessTokenProvider will use
// during API calls, ensuring MSAL caches a CAE-capable token that can be found silently later.
var token = await tokenCredential.GetTokenAsync(new TokenRequestContext(GetScopes(authContext), isCaeEnabled: true), cancellationToken).ConfigureAwait(false);
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

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

This change is addressing a subtle caching/behavioral issue (CAE token caching vs non-CAE). There doesn’t appear to be a unit test covering the TokenRequestContext used during sign-in, so this could regress silently. Consider adding a test that exercises AuthenticateAsync/SignInAsync with a stub TokenCredential and asserts GetTokenAsync is called with isCaeEnabled: true (or an equivalent observable effect) to lock in the expected behavior for -UseDeviceCode scenarios.

Copilot uses AI. Check for mistakes.
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.

Connect-MgGraph auth token unusable when -UseDeviceCode

2 participants