azure-sdk-for-net: [BUG] System.InvalidOperationException: No subscriptions found for the given credentials

Library name and version

Azure.ResourceManager.Resources 1.0.0-beta.5

Describe the bug

My program reports an error saying that I don’t have subscriptions, but I do have a subscription.

Expected behavior

My program should print the Id of my subscription.

Actual behavior

It reports an error saying that I don’t have subscriptions, but I do have a subscription.

Unhandled exception. System.InvalidOperationException: No subscriptions found for the given credentials
   at Azure.ResourceManager.ArmClient.GetDefaultSubscriptionAsync(CancellationToken cancellationToken)
   at Program.<Main>$(String[] args) in /home/adrian/temp/AZ-204/consoleApp/Program.cs:line 6
   at Program.<Main>(String[] args)

Reproduction Steps

  1. Create a new console project with dotnet new console
  2. Add the following NuGet packages:
  3. Copy and paste this code:
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Resources;

var armClient = new ArmClient(new InteractiveBrowserCredential());
var subscription = await armClient.GetDefaultSubscriptionAsync();
Console.WriteLine(subscription.Id);

This command demonstrates that I have multiple subscriptions:

$ az account list --all --query '[].{isDefault: isDefault, name: name, state: state}'
[
  {
    "isDefault": false,
    "name": "Pase para Azure: patrocinio",
    "state": "Disabled"
  },
  {
    "isDefault": true,
    "name": "Pase para Azure: patrocinio",
    "state": "Enabled"
  }
]

I think the problem must be with InteractiveBrowserCredential, because when I use DefaultAzureCredential instead and I execute dotnet run in the same terminal I executed az login I get the subscription id without problems.

Environment

$ dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.101
 Commit:    ef49f6213a

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  20.04
 OS Platform: Linux
 RID:         ubuntu.20.04-x64
 Base Path:   /usr/share/dotnet/sdk/6.0.101/

Host (useful for support):
  Version: 6.0.1
  Commit:  3a25a7f1cc

.NET SDKs installed:
  6.0.101 [/usr/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.1 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.1 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download

I am using WSL. VSCode version: 1.63.2

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 2
  • Comments: 46 (19 by maintainers)

Most upvoted comments

@christothes It’s a GitHub account actually.

Hi @adriancuadrado - It seems that you are using a Live account, if the tenantId is F8CDEF31-A31E-4B4A-93E4-5F571E91255A (live.com tenant id).

Are you logging into an AAD tenant in which this live account is guested? If so, the issue is that AAD is defaulting to this live account tenant, and you’ll need to tell it which tenantId you intend to target. If this account were a native AAD account to the target tenant, I believe you would not need to specify the tenantId.

I was having this same issue and couldn’t figure out why even when I see the tenant with GetTenants(), doing a tenant.GetSubscriptions() it doesn’t refer to the subscriptions of THAT TENANT which one would logically assume. I had to specify:

new ArmClient(new InteractiveBrowserCredential(new InteractiveBrowserCredentialOptions() { TenantId = "<<home tenant ID returned by az login>>" }))

And then I got to see those subscriptions.