如何使用Microsoft.Azure.ResourceManager列出订阅? [英] How to list subscriptions with Microsoft.Azure.ResourceManager?

查看:109
本文介绍了如何使用Microsoft.Azure.ResourceManager列出订阅?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的核心目标是用C#编写Azure WebApps部署工具.该过程将大致

My core goal is to write an Azure WebApps deployment tool in C#. The process will be roughly

  1. 用户登录
  2. 用户选择订阅
  3. 用户选择或创建资源组
  4. 用户选择或创建Web应用程序的存储空间
  5. 用户选择或创建Web服务计划
  6. 用户选择或创建Web应用
  7. 工具使用Kudu将网络应用上传到发布zip
  1. User logs in
  2. User selects subscription
  3. User selects or creates resource group
  4. User selects or creates storage for the web app
  5. User selects or creates web service plan
  6. User selects or creates web app
  7. Tool uploads the web app using Kudu to POST a zip

由于无法在门户中完成最后一步,所以我的想法是使用GUI工具进行所有操作.

Since the last step can't be done in the portal, my idea was to do everything in a GUI tool.

我开始使用Kudu的 ARMClient.Authentication Brady Gaster的博客文章.

I started out using Kudu's ARMClient.Authentication and Microsoft.Azure.ResourceManager 1.0.0-preview. However, when it comes to creating a storage account I get a permissions error (The subscription is not registered to use namespace Microsoft.Storage), so my plan B was to do the authentication myself following Brady Gaster's blog post.

我已经按照文档设置了应用程序,并使用其clientIdtenantId能够登录并列出租户.但是我无法列出任何订阅. (注意,如果将全部提供全部内容存在安全隐患,我会部分删除clientIdtenantId.)

I've set up an application as documented, and using its clientId and tenantId I'm able to log in and list tenants. But I can't list any subscriptions. (NB I've partly elided the clientId and tenantId in case there are security risks with giving them in full).

        string clientId = "f62903b9-ELIDED";
        string tenantId = "47b6e6c3-ELIDED";
        const string redirectUri = "urn:ietf:wg:oauth:2.0:oob";
        const string baseAuthUri = "https://login.microsoftonline.com/";
        const string resource = "https://management.core.windows.net/";

        var ctx = new AuthenticationContext(baseAuthUri + tenantId);
        var authResult = ctx.AcquireToken(resource, clientId, new Uri(redirectUri), PromptBehavior.Auto);
        var token = new TokenCredentials(authResult.AccessToken);
        var subClient = new SubscriptionClient(token);

        var tenants = await subClient.Tenants.ListAsync();
        foreach (var tenant in tenants) Console.WriteLine(tenant.TenantId);

        var subs = await subClient.Subscriptions.ListAsync();
        foreach (var sub in subs) Console.WriteLine(sub.DisplayName);

运行此程序时,它会提示我登录,并列出与我拥有或共同管理的订阅相对应的租户.但是它没有列出单个订阅.如果我将ID更改为常用的值(我正式认为是Powershell)

When I run this it prompts me to login, and lists the tenants corresponding to the subscriptions I own or co-administer. But it doesn't list a single subscription. If I change the IDs to the commonly used (I think officially for Powershell) values

        clientId = "1950a258-227b-4e31-a9cf-717495945fc2";
        tenantId = "common";

那是一样的.

要获得订阅列表,我错过了什么步骤?

What is the step I've missed in order to get a list of my subscriptions?

推荐答案

您需要遍历租户,在租户中进行身份验证并获取每个租户的订阅列表.

You need to iterate through the tenants, authenticate in tenant and get a subscription list for every tenant.

以下代码将像Get-AzureRmSubscription powershell cmdlet一样输出订阅.

The following code will output the Subscriptions like Get-AzureRmSubscription powershell cmdlet does.

class Program
{
    private static string m_resource = "https://management.core.windows.net/";
    private static string m_clientId = "1950a258-227b-4e31-a9cf-717495945fc2"; // well-known client ID for Azure PowerShell
    private static string m_redirectURI = "urn:ietf:wg:oauth:2.0:oob"; // redirect URI for Azure PowerShell

    static void Main(string[] args)
    {
        try
        {
            var ctx = new AuthenticationContext("https://login.microsoftonline.com/common");
            // This will show the login window
            var mainAuthRes = ctx.AcquireToken(m_resource, m_clientId, new Uri(m_redirectURI), PromptBehavior.Always);
            var subscriptionCredentials = new TokenCloudCredentials(mainAuthRes.AccessToken);
            var cancelToken = new CancellationToken();
            using (var subscriptionClient = new SubscriptionClient(subscriptionCredentials))
            {
                var tenants = subscriptionClient.Tenants.ListAsync(cancelToken).Result;
                foreach (var tenantDescription in tenants.TenantIds)
                {
                    var tenantCtx = new AuthenticationContext("https://login.microsoftonline.com/" + tenantDescription.TenantId);
                    // This will NOT show the login window
                    var tenantAuthRes = tenantCtx.AcquireToken(
                        m_resource,
                        m_clientId,
                        new Uri(m_redirectURI),
                        PromptBehavior.Never,
                        new UserIdentifier(mainAuthRes.UserInfo.DisplayableId, UserIdentifierType.RequiredDisplayableId));
                    var tenantTokenCreds = new TokenCloudCredentials(tenantAuthRes.AccessToken);
                    using (var tenantSubscriptionClient = new SubscriptionClient(tenantTokenCreds))
                    {
                        var tenantSubscriptioins = tenantSubscriptionClient.Subscriptions.ListAsync(cancelToken).Result;
                        foreach (var sub in tenantSubscriptioins.Subscriptions)
                        {
                            Console.WriteLine($"SubscriptionName : {sub.DisplayName}");
                            Console.WriteLine($"SubscriptionId   : {sub.SubscriptionId}");
                            Console.WriteLine($"TenantId         : {tenantDescription.TenantId}");
                            Console.WriteLine($"State            : {sub.State}");
                            Console.WriteLine();
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        finally
        {
            Console.WriteLine("press something");
            Console.ReadLine();
        }
    }
}

这篇关于如何使用Microsoft.Azure.ResourceManager列出订阅?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆