<p>I'm trying to create a Kubernetes cluster using Azure Management API.</p>

<pre><code>  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();
</code></pre>

<p>In the last line, a CloudException is thrown with the message: Subscription [] could not be found.</p>

<p>Even though an exception is thrown, the resource group is created but it is empty.</p>

<p>I have logged-in using Azure CLI with that service principal and I have run </p>

<pre><code>az account list
</code></pre>

<p>with the following response:</p>

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

<p>The App registration exists In Azure Active Directory > App registrations > All apps. I even gave permissions to all possible APIs.</p>

<p>Is there anything I did wrong in order to receive that exception message?</p>

<p>According to the error log, it seems you don't set default subscription for your service principal. You could use <code>az account set --subscription &lt;name or id&gt;</code> to set it.</p>

<p>If it still does not work, I suggest you could use the following code.</p>

<pre><code>  var azure = Azure
    .Configure()
    .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
    .Authenticate(credentials)
    .withSubscription("subscription id")
</code></pre>

<p>Note: You should give your service principal Owner role on your subscription level. See this <a href="https://learn.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal#assign-application-to-role" rel="nofollow noreferrer">link</a>. But it seems you had done it, but I suggest you could check again.</p>
