在Azure管理API中找不到订阅

Ciu*_*rin 5 azure azure-management kubernetes azure-container-service

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

  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();
Run Code Online (Sandbox Code Playgroud)

在最后一行中,将引发CloudException消息:找不到订阅[]。

即使引发异常,也会创建资源组,但资源组为空。

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

az account list
Run Code Online (Sandbox Code Playgroud)

响应如下:

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

应用程序注册位于Azure Active Directory>应用程序注册>所有应用程序中。我什至授予了所有可能的API的权限。

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

Shu*_*bao 2

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

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

  var azure = Azure
    .Configure()
    .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
    .Authenticate(credentials)
    .withSubscription("subscription id")
Run Code Online (Sandbox Code Playgroud)

注意:您应该在您的订阅级别上授予您的服务主体所有者角色。请参阅此链接。不过看来你已经做到了,但我建议你再检查一下。