0

Just getting started with Resource Manager in Visual Studio, but I cannot clear the first hurdle.

When I use the following code, client.GetDefaultSubscriptionAsync() returns the following error: System.InvalidOperationException: 'No subscriptions found for the given credentials'

I am using my Azure/Microsoft account to log into Visual Studio, and it the same account selected under Azure Service Authentication in VS.

using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager.Compute;
using System;
using System.Threading.Tasks;
using Azure;
using Azure.ResourceManager.Network;

ArmClient client = new ArmClient(new DefaultAzureCredential());

SubscriptionResource subscription = await client.GetDefaultSubscriptionAsync();

Seems like an incredibly basic first step but nothing I have found online has helped. What have I missed?

MWK
  • 3
  • 3
  • is your account associated to multiple tenant ?Can you try specifying the tenantId when instantiating the `DefaultAzureCredential` class? – Thomas Apr 30 '23 at 19:39
  • I think it is only assigned to one: https://i.imgur.com/ujSjpEF.png I also tried adding this, but had the same results. var DACO = new DefaultAzureCredentialOptions() { TenantId = "****************************" }; ArmClient client = new ArmClient(new DefaultAzureCredential(DACO)); – MWK Apr 30 '23 at 20:33
  • when you receive the error, would you be able to check what is the access token receive ? Which tenant is it connected with ? – Thomas Apr 30 '23 at 21:03
  • I wrote the following code, but it gave me the id of the tenant shown above: DefaultAzureCredential defaultAzureCredential = new DefaultAzureCredential(); string resource = "https://management.azure.com/"; AccessToken accessToken = await defaultAzureCredential.GetTokenAsync(new TokenRequestContext(new[] { resource + ".default" })); JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler(); JwtSecurityToken jwtToken = tokenHandler.ReadJwtToken(accessToken.Token); string tenantId = jwtToken.Claims.FirstOrDefault(claim => claim.Type == "tid")?.Value; – MWK Apr 30 '23 at 22:24
  • so does the tenantid sounds right for you ? – Thomas Apr 30 '23 at 23:08
  • Yes, it matches the account I am signed into VS with. It has a PAYG Subscription, in Azure. – MWK May 01 '23 at 10:51

1 Answers1

1

Need to check below:

  • The issue here is that you might be using personal user as a general Microsoft account, which is "external" to the tenant.

  • To make it work, create a new user in the default Azure AD directory and give necessary admin role permissions to the respective user.

    Steps clearly detailed in this SO worked by me.

Once you are done with all the above steps, then try logging in with the newly created user credentials and it will work as expected.

  • If still the issue persists, try logging out from Visual Studio and relog in/re-authenticate with Azure. It clears cache sometimes.

    Refer this SO worked by me for more Visual Studio "No subscription found" issues.

Or else

Goto Tools >> Options >> Account selection in Azure Service Authentication and remove all the existing accounts. Now login with the desired account credentials as shown.

enter image description here

Jahnavi
  • 3,076
  • 1
  • 3
  • 10
  • Great response thank you. I have this working now. I thought I had tried these steps, but I think clearing the VS accounts, restarting VS, then logging into VS with the new AD account in that order is what I did not do. – MWK May 04 '23 at 17:23