Skip to content

Commit 4318d0c

Browse files
include caeEnabled: true so token is correctly cached (#3573)
* include caeEnabled: true so token is correctly cached * add test and replace tryCatch with null check * adjust null check and cae tests * Update src/Authentication/Authentication.Test/Helpers/AuthenticationHelpersTests.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/Authentication/Authentication.Core/Utilities/AuthenticationHelpers.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * auth helper test * Update src/Authentication/Authentication.Test/Helpers/AuthenticationHelpersTests.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/Authentication/Authentication.Test/Helpers/AuthenticationHelpersTests.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * remove redundant catch --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 643f9be commit 4318d0c

File tree

2 files changed

+682
-542
lines changed

2 files changed

+682
-542
lines changed

src/Authentication/Authentication.Core/Utilities/AuthenticationHelpers.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,19 @@ private static async Task<DeviceCodeCredential> GetDeviceCodeCredentialAsync(IAu
160160
TokenCachePersistenceOptions = GetTokenCachePersistenceOptions(authContext),
161161
DeviceCodeCallback = (code, cancellation) =>
162162
{
163-
GraphSession.Instance.OutputWriter.WriteObject(code.Message);
163+
if (GraphSession.Exists)
164+
{
165+
try
166+
{
167+
GraphSession.Instance.OutputWriter.WriteObject(code.Message);
168+
return Task.CompletedTask;
169+
}
170+
catch (InvalidOperationException)
171+
{
172+
// Fall through to console output if OutputWriter is unavailable.
173+
}
174+
}
175+
Console.WriteLine(code.Message);
164176
return Task.CompletedTask;
165177
}
166178
};
@@ -272,12 +284,14 @@ public static async Task<IAuthContext> AuthenticateAsync(IAuthContext authContex
272284
return signInAuthContext;
273285
}
274286

275-
private static async Task<IAuthContext> SignInAsync(IAuthContext authContext, CancellationToken cancellationToken = default)
287+
internal static async Task<IAuthContext> SignInAsync(IAuthContext authContext, CancellationToken cancellationToken = default, TokenCredential tokenCredential = null)
276288
{
277289
if (authContext is null)
278290
throw new AuthenticationException(ErrorConstants.Message.MissingAuthContext);
279-
var tokenCredential = await GetTokenCredentialAsync(authContext, cancellationToken).ConfigureAwait(false);
280-
var token = await tokenCredential.GetTokenAsync(new TokenRequestContext(GetScopes(authContext)), cancellationToken).ConfigureAwait(false);
291+
tokenCredential ??= await GetTokenCredentialAsync(authContext, cancellationToken).ConfigureAwait(false);
292+
// Use isCaeEnabled: true to match the TokenRequestContext that AzureIdentityAccessTokenProvider will use
293+
// during API calls, ensuring MSAL caches a CAE-capable token that can be found silently later.
294+
var token = await tokenCredential.GetTokenAsync(new TokenRequestContext(GetScopes(authContext), isCaeEnabled: true), cancellationToken).ConfigureAwait(false);
281295
JwtHelpers.DecodeJWT(token.Token, account: null, ref authContext);
282296
return authContext;
283297
}

0 commit comments

Comments
 (0)