在 Azure 管理中找不到订阅 API

Subscription could not be found in Azure Management API

我正在尝试使用 Azure 管理创建 Kubernetes 集群 API。

  var credentials = SdkContext.AzureCredentialsFactory
    .FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"));


  var azure = Azure
    .Configure()
    .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
    .Authenticate(credentials)
    .WithDefaultSubscription();

var kubernetesCluster = azure.KubernetesClusters.Define("aks").WithRegion(Region.EuropeWest)
        .WithNewResourceGroup("aksResourceGroup").WithLatestVersion().WithRootUsername("aksUsername")
        .WithSshKey(sshPublicKey).WithServicePrincipalClientId("clientId")
        .WithServicePrincipalSecret("secret").DefineAgentPool("ap").WithVirtualMachineCount(1)
        .WithVirtualMachineSize(ContainerServiceVirtualMachineSizeTypes.StandardA0).Attach()
        .WithDnsPrefix("dns-aks").Create();

在最后一行中,抛出了 CloudException 和消息:找不到订阅 []。

即使抛出异常,创建资源组也是空的

我已经使用带有该服务主体的 Azure CLI 登录并且我有 运行

az account list

响应如下:

[
  {
    "cloudName": "AzureCloud",
    "id": "SUBSCRIPTION ID FROM EXCEPTION ABOVE",
    "isDefault": true,
    "name": "Pay-As-You-Go",
    "state": "Enabled",
    "tenantId": "xxx",
    "user": {
      "name": "xxxx",
      "type": "servicePrincipal"
    }
  }
]

应用程序注册存在于 Azure Active Directory > 应用程序注册 > 所有应用程序中。我什至授予所有可能的 APIs.

权限

为了收到异常消息,我做错了什么吗?

根据错误日志,您似乎没有为您的服务主体设置默认订阅。您可以使用 az account set --subscription <name or id> 来设置它。

如果还是不行,我建议你可以使用下面的代码。

  var azure = Azure
    .Configure()
    .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
    .Authenticate(credentials)
    .withSubscription("subscription id")

注意:您应该在您的订阅级别上授予您的服务主体所有者角色。看到这个 link。不过你好像已经做到了,不过我建议你再检查一下。