Reputation:
When we use Azure CLI command az login
with Azure account, we can get all subscriptions in all tenants the user belongs to. Now I want to implement it with Azure .net sdk. But I just can get one tenant's subscriptions. Could someone help me?
var cred= SdkContext.AzureCredentialsFactory.FromDevice("app id", "common",AzureEnvironment.AzureGlobalCloud, code =>
{
Console.WriteLine(code.Message);
return true;
});
var azure = Azure.Authenticate(cred);
var subs = azure.Subscriptions.List();
foreach (var sub in subs) {
Console.WriteLine(sub.DisplayName);
}
Upvotes: 0
Views: 1191
Reputation: 42043
The azure.Subscriptions.List()
will just list the subscriptions in one tenant. Even if you use common
parameter, actually it will also specify a default tenant, not all the tenant.
You could try the code as below, it lists all the tenants and calls the REST API to list all the subscriptions in every tenant that the user can access.
Note: The client id I used is the Microsoft Application Microsoft Azure CLI
, you can use 04b07795-8ddb-461a-bbee-02f9e1bf7b46
directly, no need to change it to yours. Just run the code as below without changing anything.
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using System;
using Microsoft.Azure.Services.AppAuthentication;
using System.Net.Http;
using Newtonsoft.Json;
using System.IO;
using Newtonsoft.Json.Linq;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
var cred = SdkContext.AzureCredentialsFactory.FromDevice("04b07795-8ddb-461a-bbee-02f9e1bf7b46", "common", AzureEnvironment.AzureGlobalCloud, code =>
{
Console.WriteLine(code.Message);
return true;
});
var azure = Azure.Authenticate(cred);
var tenants = azure.Tenants.List();
foreach (var tenant in tenants)
{
string authority = "https://login.microsoftonline.com/" + tenant.TenantId;
var authContext = new AuthenticationContext(authority);
AuthenticationResult result = authContext.AcquireTokenAsync("https://management.azure.com/", "04b07795-8ddb-461a-bbee-02f9e1bf7b46", new Uri("http://localhost:80"), new PlatformParameters(PromptBehavior.Auto)).Result;
//AuthenticationResult result = authContext.AcquireTokenByDeviceCodeAsync(devcode).Result;
using (var client = new HttpClient()) {
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + result.AccessToken);
client.DefaultRequestHeaders.Accept.Clear();
//GET Method
HttpResponseMessage response = client.GetAsync("https://management.azure.com/subscriptions?api-version=2019-06-01").GetAwaiter().GetResult();
if (response.IsSuccessStatusCode)
{
//Console.WriteLine(response.Content.ReadAsStringAsync().Result.ToString());
string myjson = response.Content.ReadAsStringAsync().Result.ToString();
JObject jo1 = (JObject)JsonConvert.DeserializeObject(myjson);
string s1 = jo1["value"].ToString();
JArray ja2 = (JArray)JsonConvert.DeserializeObject(s1);
if (ja2.Count != 0)
{
for(int i=0;i<ja2.Count;i++)
{
string j1 = ja2[i]["displayName"].ToString();
Console.WriteLine(j1);
}
}
}
else
{
Console.WriteLine("Internal server Error");
}
}
}
Console.ReadLine();
}
}
}
Upvotes: 1