Skip to content

Commit

Permalink
User ChainedTokenCredential and fallback to interactive browser to login
Browse files Browse the repository at this point in the history
  • Loading branch information
wyunchi-ms committed Nov 7, 2024
1 parent aa01ff7 commit 5d0072c
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions shell/agents/Microsoft.Azure.Agent/Schema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,24 +386,16 @@ internal async Task CreateOrRenewTokenAsync(CancellationToken cancellationToken)

if (needRefresh)
{
_accessToken = await new AzureCliCredential()
_accessToken = await new ChainedTokenCredential(new AzureCliCredential(),
new InteractiveBrowserCredential())
.GetTokenAsync(_tokenContext, cancellationToken);
}
}
catch (Exception azureCliLoginException) when (azureCliLoginException is not OperationCanceledException)
catch (Exception e) when (e is not OperationCanceledException)
{
string message = $"Failed to generate the user access token using az cli and try to fallback to interactive browser: {azureCliLoginException.Message}.";
Telemetry.Trace(AzTrace.Exception(message), azureCliLoginException);
try {
_accessToken = await new InteractiveBrowserCredential()
.GetTokenAsync(_tokenContext, cancellationToken);
}
catch (Exception interactiveBrowserLoginException) when (interactiveBrowserLoginException is not OperationCanceledException)
{
message = $"Failed to generate the user access token using interactive browser: {interactiveBrowserLoginException.Message}.";
Telemetry.Trace(AzTrace.Exception(message), interactiveBrowserLoginException);
throw new TokenRequestException(message, interactiveBrowserLoginException);
}
string message = $"Failed to generate the user access token: {e.Message}.";
Telemetry.Trace(AzTrace.Exception(message), e);
throw new TokenRequestException(message, e);
}
}

Expand Down

0 comments on commit 5d0072c

Please sign in to comment.