Jeff
Jeff

Reputation: 982

Trying to Connect to Azure using Environment Credentials Not Working

I have been trying to run some sample code to connect to Axure for the sake of understanding and testing how my application will work on a server. In my "real" code, I have a C# Web API (.NET 4.8) which has a class library that kicks off an Azure Data Factory Pipeline. On my local PC, this works fine because I am using Visual Studio to authenticate to Azure.

However, I need to test how this will work on a server. So my sample code below is just pulling a secret from an Azure KeyVault as a POC (Proof of Concept). From my understanding, when using DefaultAzureCredential, it authenticates in the following order:

  1. EnvironmentCredential
  2. ManagedIdentityCredential
  3. SharedTokenCacheCredential
  4. VisualStudioCredential
  5. VisualStudioCodeCredential
  6. AzureCliCredential
  7. InteractiveBrowserCredential

I would like to test using the first option, EnvironmentCredential. Looking @ the Microsoft documentation @ EnvironmentCredential class I have a few options. For now, I need to use the last option, which requires I have the following Environment Variables set:

  1. AZURE_TENANT_ID
  2. AZURE_CLIENT_ID
  3. AZURE_USERNAME
  4. AZURE_PASSWORD

I am still receiving the error:

DefaultAzureCredential failed to retrieve a token from the included credentials. EnvironmentCredential authentication unavailable. Environment variables are not fully configured.

**

**

More supporting evidence below.

See the screenshot below where I have set up the environment variables: enter image description here

Code:

var client = new SecretClient(vaultUri: new Uri(url), credential: new DefaultAzureCredential());
var secretData = client.GetSecret("Secret");

Upvotes: 1

Views: 4012

Answers (1)

Harshitha
Harshitha

Reputation: 7377

DefaultAzureCredential failed to retrieve a token from the included credentials. EnvironmentCredential authentication unavailable. Environment variables are not fully configured.

I have set the Environment Variables under User Variables.

enter image description here

Also, set these variables in the deployed Azure App => Configuration => Application Settings.

enter image description here

  • The values which are set in the App Settings are available as Environment Variables.

enter image description here

  • To verify whether Environment Variables are set correctly, try to retrieve the Values before calling DefaultAzureCredential.
  • Comment the code related to retrieving the KeyVault and try to display the Environment Variables.

Ex:

string tenantId = Environment.GetEnvironmentVariable("APPSETTING_AZURE_TENANT_ID");

Code to retrieve secrets fro mKeyVault.

var kvURL = "https://KV15June.vault.azure.net/";
var credential = new DefaultAzureCredential();
var client = new SecretClient(new Uri(kvURL), credential);

var secret1 = client.GetSecret("Secret1").Value.Value;

As you are using Username and password: to authenticate, make sure the account which you are using is not MFA Enabled as mentioned in the MSDoc.

Upvotes: 2

Related Questions