The error messages seem to imply that environment variables were read but possibly are invalid or malformed and that Entra ID is rejecting the secret as invalid.
- Double-check you are using the secret value, not the secret ID
Go to Azure portal > Entra ID > App Registrations > Your App > Certificates & secrets.
- Secret ID = A GUID shown in the table (this is not what you use).
- Secret Value = Only visible when the secret is first created. This is what you must copy and store securely.
- Verify your env vars are correctly named and loaded
Azure SDK uses the following:
AZURE_TENANT_ID
AZURE_CLIENT_ID
AZURE_CLIENT_SECRET
Run the following in your debugger to confirm:
Console.WriteLine($"Tenant: {Environment.GetEnvironmentVariable("AZURE_TENANT_ID")}");
Console.WriteLine($"ClientId: {Environment.GetEnvironmentVariable("AZURE_CLIENT_ID")}");
Console.WriteLine($"Secret: {(string.IsNullOrEmpty(Environment.GetEnvironmentVariable("AZURE_CLIENT_SECRET")) ? "MISSING" : "SET")}");
If these are missing in the runtime process, even if they're set in the system/user environment, your app may not see them. In Visual Studio:
- Go to Project > Properties > Debug
- Ensure
AZURE_TENANT_ID
, etc., are present in "Environment variables" for the debug profile.
Alternatively, set them in your shell and run the app from CLI.
- Check if you are using
DefaultAzureCredential
in an environment where other credentials interfere
DefaultAzureCredential
will try many credential types in order:
- EnvironmentCredential
- ManagedIdentityCredential
- SharedTokenCacheCredential
- VisualStudioCredential
- etc.
Add logging to confirm which credential is failing:
var options = new DefaultAzureCredentialOptions
{
Diagnostics =
{
IsLoggingContentEnabled = true,
LoggedHeaderNames = { "x-ms-request-id" },
LoggedQueryParameters = { "api-version" },
IsAccountIdentifierLoggingEnabled = true
}
};
var credential = new DefaultAzureCredential(options);
Or simplify for testing:
var credential = new ClientSecretCredential(
Environment.GetEnvironmentVariable("AZURE_TENANT_ID"),
Environment.GetEnvironmentVariable("AZURE_CLIENT_ID"),
Environment.GetEnvironmentVariable("AZURE_CLIENT_SECRET")
);
This eliminates all other credential types that might be interfering.
If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.
hth
Marcin