in src/Authentication/MsalManagedIdentityTokenProvider.cs [22:56]
public bool CanGetToken(TokenRequest tokenRequest) =>
!string.IsNullOrWhiteSpace(tokenRequest.ClientId);
public async Task<AuthenticationResult?> GetTokenAsync(TokenRequest tokenRequest, CancellationToken cancellationToken = default)
{
try
{
if (string.IsNullOrWhiteSpace(tokenRequest.ClientId))
{
logger.LogTrace(string.Format(Resources.MsalClientIdError, tokenRequest.ClientId));
return null;
}
IManagedIdentityApplication app = ManagedIdentityApplicationBuilder.Create(CreateManagedIdentityId(tokenRequest.ClientId!))
.WithHttpClientFactory(appConfig.HttpClientFactory)
.WithLogging(appConfig.LoggingCallback, appConfig.LogLevel, appConfig.EnablePiiLogging, appConfig.IsDefaultPlatformLoggingEnabled)
.Build();
AuthenticationResult result = await app.AcquireTokenForManagedIdentity(MsalConstants.AzureDevOpsResource)
.ExecuteAsync()
.ConfigureAwait(false);
return result;
}
catch (MsalServiceException ex) when (ex.ErrorCode is MsalError.ManagedIdentityRequestFailed)
{
logger.LogTrace(ex.Message);
return null;
}
catch (MsalServiceException ex) when (ex.ErrorCode is MsalError.ManagedIdentityUnreachableNetwork)
{
logger.LogTrace(ex.Message);
return null;
}
}