Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Function: DefaultAzureCredential.GetTokenAsync fails when running locally

I have a C# azure function on .NET 6 running in dotnet-isolated mode. This function calls another azure function, which is using Azure AD Authentication. In order to generate the token I have the following code:

    var audience = $"api://{appRegistrationClientId}";           
    var tokenCredential = new DefaultAzureCredential();
    var token = await tokenCredential.GetTokenAsync(new TokenRequestContext(new[] { $"{audience}/.default" }) { });
    var apiToken = token.Token;

    return apiToken;

If I deploy this code to Azure, the code runs fine. I am able to call and retrieve data from the other function app. However, when running this locally on Visual Studio I get the following exception:

Azure PowerShell authentication failed due to an unknown error. See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/powershellcredential/troubleshoot Unhandled exception. System.ArgumentException: Startup hook assembly 'Microsoft.Azure.Functions.Worker.Core' failed to load. See inner exception for details.

---> System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Azure.Functions.Worker.Core, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

File name: 'Microsoft.Azure.Functions.Worker.Core, Culture=neutral, PublicKeyToken=null'

at System.Reflection.RuntimeAssembly.InternalLoad(ObjectHandleOnStack assemblyName, ObjectHandleOnStack requestingAssembly, StackCrawlMarkHandle stackMark, Boolean throwOnFileNotFound, ObjectHandleOnStack assemblyLoadContext, ObjectHandleOnStack retAssembly)

at System.Reflection.RuntimeAssembly.InternalLoad(AssemblyName assemblyName, RuntimeAssembly requestingAssembly, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, AssemblyLoadContext assemblyLoadContext)

at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyName(AssemblyName assemblyName)

at System.StartupHookProvider.CallStartupHook(StartupHookNameOrPath startupHook)

--- End of inner exception stack trace ---

at System.StartupHookProvider.CallStartupHook(StartupHookNameOrPath startupHook)

at System.StartupHookProvider.ProcessStartupHooks()

I tried going through the troubleshooting on the link provided by the exception message, but nothing worked. I am able to call Get-AzAccessToken -ResourceUrl https://management.core.windows.net and generate a token in powershell.

I have also added my azure account to Visual Studio under Azure Service Authentication -> Account Selection option.

Is there a way to make this call work locally or otherwise what are the workarounds for this. I do need to be able to call this function from my dev machine in order to test my own code.

like image 974
paddingtonMike Avatar asked Oct 25 '25 03:10

paddingtonMike


1 Answers

When I got this error I had to restart Visual Studio 2022 and re-enter the credentials of the Azure Service Authentication (including MFA).

Visual Studio > Tools > Options > Azure Service Authentication

like image 62
DeMaki Avatar answered Oct 26 '25 18:10

DeMaki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!