1

I recently migrated my azure functions project from .net core 3.1 to .net 6 and thus, to VS 2022. I am using the Nuget Azure.Identity package to authenticate to other azure services. When running my app from windows terminal, I can choose the right subscription/tenant using az account set -s oneOfMySubs. Then, the defaultcredential class in my code picks up the correct CLI credential from the chain as expected from MS documentation.

However, when debugging my app inside VS, I am getting error message Login failed for user '<token-identified principal>'. The server is not currently configured to accept this token.

I suspect, that VS picks up my account's default subscription, which is not the one I am running my connected services in.

How can I change the used subscription context within VS 2022?

As it seems MS has retired the Azure node in Server Explorer, , I see no chance to do so.

Tarek Salha
  • 307
  • 3
  • 12
  • The subscription doesn't matter, only the tenant does. Have you tried configuring the tenant ids to DefaultAzureCredential? You can also try using some of the CLI credentials directly to check if that particular credential is the issue or if the DefaultAzureCredential is using some other credential that you don't expect. – juunas Mar 15 '22 at 10:34
  • When debugging in vscode it works, because there I can directly use the CLI credential. What do you mean exactly with configuring tenantId to defaultcredential? Use local.settings? – Tarek Salha Mar 16 '22 at 13:19
  • 1
    I mean that you can construct a credential like this: `new DefaultAzureCredential(new DefaultAzureCredentialOptions { })` and in the options there are several properties that allow you to define the tenant id for e.g. Visual Studio authentication. – juunas Mar 16 '22 at 13:47

1 Answers1

1

Thanks @juunas for your help. This is the result, that I came up with and your comment was very useful to get there.

var credential = new DefaultAzureCredential(new DefaultAzureCredentialOptions()
{
    VisualStudioTenantId = config.TenantId 
});
var tokenRequestContext = new Azure.Core.TokenRequestContext(new string[] { "https://management.core.windows.net/" });
var token = await credential.GetTokenAsync(tokenRequestContext);
var _credentials = new TokenCredentials(token.Token);

Later, I am using this token to authorize to some azure services

var client = new DataFactoryManagementClient(_credentials)

var factories = await client.Factories.ListByResourceGroupAsync(config.ResourceGroupName);
Tarek Salha
  • 307
  • 3
  • 12