nam
nam

Reputation: 23868

Azure error: DefaultAzureCredential authentication failed

I am working on the Official Azure sample: Getting started - Managing Compute Resources using Azure .NET SDK. And getting the following error on line resourceGroup = await resourceGroups.CreateOrUpdateAsync(resourceGroupName, resourceGroup); of the following code where app is trying to create a Resource Group. I have followed the instructions for Registering an app and from this link provided by the sample. And, have assigned a role to app as follows:

enter image description here

Error:

Azure.Identity.AuthenticationFailedException HResult=0x80131500 Message=DefaultAzureCredential authentication failed. Source=Azure.Identity

Inner Exception 2: MsalServiceException: AADSTS70002: The client does not exist or is not enabled for consumers. If you are the application developer, configure a new application through the App Registrations in the Azure Portal

static async Task Main(string[] args)
{
    var subscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID");
    var resourceClient = new ResourcesManagementClient(subscriptionId, new DefaultAzureCredential());

    // Create Resource Group
    Console.WriteLine("--------Start create group--------");
    var resourceGroups = resourceClient.ResourceGroups;
    var location = "westus2";
    var resourceGroupName = "QuickStartRG";
    var resourceGroup = new ResourceGroup(location);
    resourceGroup = await resourceGroups.CreateOrUpdateAsync(resourceGroupName, resourceGroup);
    Console.WriteLine("--------Finish create group--------");

    // Create a Virtual Machine
    await Program.CreateVmAsync(subscriptionId, "QuickStartRG", location, "quickstartvm");

    // Delete resource group if necessary
    //Console.WriteLine("--------Start delete group--------");
    //await (await resourceGroups.StartDeleteAsync(resourceGroupName)).WaitForCompletionAsync();
    //Console.WriteLine("--------Finish delete group--------");
    //Console.ReadKey();
}

UPDATE:

As per instructions in the sample, following is how I Used the portal to create an Azure AD application and service principal that can access resources. I may not have done something right here. Please let me know what I am not doing right here:

Role Assignment for the registered app in Access Control (IAM):

enter image description here

Authentication and Direct URI:

enter image description here

API Permissions for the Registered App:

enter image description here

UPDATE-2:

Working with @JoyWan, I was able to resolve the issue (thank you Joy). Below is the screenshot of successful creation of all required compute resources including VM. NOTE: Clicking on the image would provide a better view of the screenshot.

enter image description here

Upvotes: 1

Views: 25702

Answers (1)

Joy Wang
Joy Wang

Reputation: 42133

I test the code, it works fine on my side. The steps you mentioned are also correct.

In this sample, the DefaultAzureCredential() actually uses the EnvironmentCredential() in local, so if you run the code in local, make sure you have Set Environment Variables with the AD App Client ID, Client Secret, Tenant ID.

enter image description here

enter image description here

enter image description here

Update:

From @nam's comment, the issue was that environment vars were not refreshed yesterday, since he had shutdown the machine yesterday and restarted it again today, the environment var got in sync and hence the app started working.

Upvotes: 2

Related Questions