Tarek Salha
Tarek Salha

Reputation: 384

Change Azure Subscription/tenant in Visual Studio 2022

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.

Upvotes: 3

Views: 5124

Answers (1)

Tarek Salha
Tarek Salha

Reputation: 384

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);

Upvotes: 1

Related Questions