Using DefaultAzureCredentials on console app in development environment

Espen Johannessen 76 Reputation points
2020-11-17T13:29:37.337+00:00

We are having issues when developing code that uses the DefaultAzureCredential class when connecting to storage account blobs among other Azure services from our local machines.

It works fine when deployed to Azure, but on our local machines in our development environment a call like this...

BlobServiceClient blobServiceClient = new BlobServiceClient(new Uri(storageAccountUrl), new DefaultAzureCredential());  

...just hangs and eventually does a timeout with an exception saying

{"ManagedIdentityCredential authentication failed."} with an 504 error

I know this credential uses a prioritized "pecking order" to try different credentials (https://learn.microsoft.com/en-us/dotnet/api/overview/azure/identity-readme), so we tried disabling the two first in the chain. This made things work and we were able to login:

var credentialOptions = new DefaultAzureCredentialOptions();  
if (ConfigurationManager.AppSettings["IsDevelopmentEnvironment"].Equals("true"))  
{  
        credentialOptions.ExcludeEnvironmentCredential = true;  
        credentialOptions.ExcludeManagedIdentityCredential = true;  
 }  
  
 BlobServiceClient blobServiceClient = new BlobServiceClient(new Uri(storageAccountUrl), new DefaultAzureCredential(credentialOptions));  

But we should not need to have code like this, and this will prevent the app from working in Azure. This contradicts the whole point of this class.

Does anyone have insight into how we can make this work more seamless in our development environment?

Microsoft Security | Microsoft Entra | Microsoft Entra ID
{count} votes

1 answer

Sort by: Most helpful
  1. Espen Johannessen 76 Reputation points
    2020-11-18T14:18:12.557+00:00

    anonymous user-msft I see in Fiddler that there are four requests like the one described under going before the code gives up and gives an exception:

    GET /metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fxxx-xxxx-xxxxx.azconfig.io HTTP/1.1
    Metadata: true
    x-ms-client-request-id: 6700f178-af2e-4792-9a2f-5f43c382c216
    x-ms-return-client-request-id: true
    User-Agent: azsdk-net-Identity/1.1.1 (.NET Framework 4.8.4250.0; Microsoft Windows 10.0.19041 )
    Request-Id: |ce277704-41db6514f91cf479.
    Host: 169.254.169.254
    Connection: Keep-Alive
    

    The reponse is:

    <html><head><title>504 Gateway Timeout</title></head>
    <body><h1>Gateway Timeout</h1>
    <p>Server error - server 169.254.169.254 is unreachable at this moment.<br><br>Please retry the request or contact your administrator.<br></p>
    <!--Zscaler/6.0--></body></html>
    

    When running the code with the workaround explained above I only see calls to login.microsoft.com.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.