gpt4 book ai didi

azure-active-directory - 如何使用 Microsoft.Azure.ResourceManager 列出订阅?

转载 作者:行者123 更新时间:2023-12-01 13:44:54 24 4
gpt4 key购买 nike

语境

我的核心目标是用 C# 编写 Azure WebApps 部署工具。过程大概是

  • 用户登录
  • 用户选择订阅
  • 用户选择或创建资源组
  • 用户为 Web 应用程序选择或创建存储
  • 用户选择或创建 Web 服务计划
  • 用户选择或创建 Web 应用程序
  • 工具使用 Kudu 将 Web 应用程序上传到 POST a zip

  • 由于最后一步无法在门户中完成,我的想法是在 GUI 工具中完成所有操作。

    我开始使用 Kudu 的 ARMClient.AuthenticationMicrosoft.Azure.ResourceManager 1.0.0-preview .但是,在创建存储帐户时,出现权限错误(订阅未注册为使用命名空间 Microsoft.Storage),因此我的计划 B 是按照 Brady Gaster's blog post 自己进行身份验证。 .

    问题

    我已经按照文档设置了一个应用程序,并使用了它的 clientIdtenantId我能够登录并列出租户。但我无法列出任何订阅。 (注意,我已经部分省略了 clientIdtenantId,以防全额提供它们存在安全风险)。

            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 的正式值)
            clientId = "1950a258-227b-4e31-a9cf-717495945fc2";
    tenantId = "common";

    那么它是一样的。

    为了获取我的订阅列表,我错过了什么步骤?

    最佳答案

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

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

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

    关于azure-active-directory - 如何使用 Microsoft.Azure.ResourceManager 列出订阅?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36689689/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com