获取一个用户所属的所有租户中的所有订阅

时间:2019-11-01 13:45:32

标签: .net azure

当我们对Azure帐户使用Azure CLI命令public static List<String> getFirstNames(List<String> names){ return names.stream() .map(x->Arrays.asList(x.split("\\s[A-Za-z]+"))) .flatMap(Collection::stream) .collect(Collectors.toList()); } 时,我们可以获取用户所属的所有租户中的所有订阅。现在,我想使用Azure .net sdk来实现它。但是我只能得到一个租户的订阅。有人可以帮我吗?

public static List<String> getFirstNames(List<String> names){
    return names.stream()
            .map(x->x.split("\\s+")[0])
            .collect(Collectors.toList());
}

1 个答案:

答案 0 :(得分:1)

azure.Subscriptions.List()只会在一个租户中列出订阅。即使您使用common参数,实际上它也会指定默认租户,而不是所有租户。

您可以尝试下面的代码,它列出所有租户并调用REST API以列出用户可以访问的每个租户中的所有订阅。

注意:我使用的客户端ID是Microsoft应用程序Microsoft Azure CLI,您可以直接使用04b07795-8ddb-461a-bbee-02f9e1bf7b46,而无需将其更改为您的应用程序。只需运行以下代码,无需进行任何更改即可。

enter image description here

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();

        }
    }
}

enter image description here