Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,23 @@ private static async Task<DeviceCodeCredential> GetDeviceCodeCredentialAsync(IAu
TokenCachePersistenceOptions = GetTokenCachePersistenceOptions(authContext),
DeviceCodeCallback = (code, cancellation) =>
{
GraphSession.Instance.OutputWriter.WriteObject(code.Message);
if (GraphSession.Exists)
{
try
{
GraphSession.Instance.OutputWriter.WriteObject(code.Message);
return Task.CompletedTask;
}
catch (InvalidOperationException)
{
// Fall through to console output if OutputWriter is unavailable.
}
catch (ObjectDisposedException)
{
// Fall through to console output if OutputWriter is unavailable.
}
}
Console.WriteLine(code.Message);
return Task.CompletedTask;
}
};
Expand Down Expand Up @@ -272,12 +288,14 @@ public static async Task<IAuthContext> AuthenticateAsync(IAuthContext authContex
return signInAuthContext;
}

private static async Task<IAuthContext> SignInAsync(IAuthContext authContext, CancellationToken cancellationToken = default)
internal static async Task<IAuthContext> SignInAsync(IAuthContext authContext, CancellationToken cancellationToken = default, TokenCredential tokenCredential = null)
{
if (authContext is null)
throw new AuthenticationException(ErrorConstants.Message.MissingAuthContext);
var tokenCredential = await GetTokenCredentialAsync(authContext, cancellationToken).ConfigureAwait(false);
var token = await tokenCredential.GetTokenAsync(new TokenRequestContext(GetScopes(authContext)), cancellationToken).ConfigureAwait(false);
tokenCredential ??= await GetTokenCredentialAsync(authContext, cancellationToken).ConfigureAwait(false);
// 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);
JwtHelpers.DecodeJWT(token.Token, account: null, ref authContext);
return authContext;
}
Expand Down
Loading
Loading