I have an ASP.NET Core Web Api in Azure that supports Azure AD authentication. I have an Azure TimerTrigger function that is attempting to retrieve a token using the ITokenAcquisition .GetAccessTokenForAppAsync() method.
private async Task PrepareAuthenticatedClient()
{
var scope = "api://ClientId-Guid/.default";
try
{
var accessToken = await _tokenAcquisition.GetAccessTokenForAppAsync(scope);
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
catch (Exception ex)
{
Console.WriteLine(ex);
throw;
}
}
When I do this the function app stops running with an Access Violation code.
Between my appsettings.json and secrets.json I believe I have the necessary AzureAd parameters necessary.
What would cause an Access Violation in this case? Is there a reason why a more meaningful message isn't returned?
I have tried running locally using Azurite and against a valid storage account.
I have attempted inputting the scope parameter in different forms:
api:///ReadAccess
api:///ReadAccess/.default
ReadAccess
It was proposed that the _tokenAcquisition maybe null which could be causing the violation. Here is what is in that object at the time of requesting the token.