【问题标题】:How to read domain of Azure Active Directory如何读取 Azure Active Directory 的域
【发布时间】:2019-02-26 19:05:55
【问题描述】:

我创建了新的 Azure 帐户并尝试使用以下代码在其中自动部署应用程序:

var app = azure.AccessManagement.ActiveDirectoryApplications
.Define(appName)
.WithSignOnUrl(appSignOnUrl)
.WithAvailableToOtherTenants(true)
.WithIdentifierUrl(identifierUrl)
.DefinePasswordCredential(username)
.WithPasswordValue(password)
.WithDuration()
.Attach();
.CreateAsync();

如果 identifierUrl 被硬编码为 Azure Active Directory 名称,它就可以工作。

如何从 Azure 读取 identifierUrl(Azure Active Directory 域名)?

我可以在门户中看到这个值,但我找不到读取它的 API。

【问题讨论】:

    标签: c# azure-active-directory


    【解决方案1】:

    获取与 Azure AD 租户关联的域名的代码

    请注意,您的租户可以有多个域名。您在屏幕截图中显示的问题只是第一个在创建 Azure AD 时分配给您的租户的问题,并且由于它使用 .onmicrosoft.com 已经过验证。 Link

    您始终可以将其他域与您的 Azure AD 租户相关联,您可以证明其所有权并对其进行验证。稍后我会稍微介绍一下,但首先是相关代码。在您的情况下,您可能只会返回一个默认域。

    这是我使用 Azure AD 租户快速编写和测试的工作代码。由于您已经在使用 fluent API 来创建应用程序,因此应该非常相似。

    我在一个简单的控制台应用程序中使用了 .NET 和 C#,但我猜想其他库的代码也会非常相似。

    using System;
    using Microsoft.Azure.Management.Fluent;
    using Microsoft.Azure.Management.Graph.RBAC.Fluent;
    using Microsoft.Azure.Management.ResourceManager.Fluent;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                // whatever method you're using already for Authentication (like through file or with credentials or with cert
                // same can be used to get AzureCredentials as well, just change the FromFile to FromServicePrincipal if required
                IAzure azure = Azure.Authenticate("my.azureauth").WithDefaultSubscription();
                var creds = SdkContext.AzureCredentialsFactory.FromFile("my.azureauth");
    
                IGraphRbacManager graphRbacManager = GraphRbacManager.Authenticate(creds, "<your tenant Guid>");    
                var domains = graphRbacManager.Inner.Domains.ListAsync().GetAwaiter().GetResult();
    
                string defaultDomain = string.Empty;
                foreach (var domain in domains)
                {  
                    Console.WriteLine(domain.Name);
                    if (domain.IsDefault.HasValue && domain.IsDefault.Value == true)
                        defaultDomain = domain.Name;                
                        // not breaking out of loop on purpose, just to print all domain names if multiple are there.
                }
    
                string identiferUri = string.Format("https://{0}/myuniqueapp1", defaultDomain);
                var app = azure.AccessManagement.ActiveDirectoryApplications
                    .Define("My Unique App 1")
                    .WithSignOnUrl("https://myuniqueapp1.azurewebsites.net")
                    .WithAvailableToOtherTenants(true)
                    .WithIdentifierUrl(identiferUri)
                    .DefinePasswordCredential("string")
                    .WithPasswordValue("string")
                    .WithDuration(new TimeSpan(365,0,0,0))
                    .Attach()
                    .CreateAsync();
    
                Console.ReadLine();
            }        
        }
    }
    

    identifierUris 以及与 Azure AD 租户的已验证域的关系

    在您创建应用程序的代码中,您执行.WithIdentifierUrl(identifierUrl) 它进入并将提供的Url 添加到您的应用程序清单的identifierUris 集合。在 Azure 门户中,您将看到在应用注册的属性 > 应用 ID URI 中指定的此值。您还可以编辑清单并直接在门户中查看。

    此值唯一标识您的应用程序。对于单租户应用程序,您可以将其设置为 Azure AD 中任何其他应用程序未使用的任何唯一值,但对于多租户应用程序,它必须在全局范围内强制执行,因此限制使用主机所在的 URL name 与您的 Azure AD 租户的已验证域之一匹配。由于您使用的是.WithAvailableToOtherTenants(true),因此这个概念与您相关。

    这里有几个关于 Microsoft Docs 的链接讨论这个 -

    需要权限

    希望您已经涵盖了此步骤,因为您需要创建应用程序的权限,但如果您没有或其他人将来阅读此内容,因为代码正在从 Azure AD 读取信息并创建新应用程序在 Azure AD 中,用于获取 AzureCredentials 以运行此代码的服务主体应该具有足够的权限。

    转到您的 Azure AD > 应用程序注册 > 为您的服务主体注册应用程序(您可以通过应用程序 ID 找到它,它将与您的服务主体具有相同的应用程序 ID)> 转到所需的权限 > 添加 Windows Azure Active Directory 和为您的代码提供适当的应用程序权限。

    最后,请务必执行“授予权限”,因为此处的所有应用程序权限都需要管理员同意。

    【讨论】:

      【解决方案2】:

      您似乎只是想读取租户名称。您可以通过调用获取您登录的租户的名称

      https://management.azure.com/tenants?$skiptoken={skiptoken}&api-version={api-version}
      

      详情请参阅this page。这将为您提供您授权的所有租户的列表。

      【讨论】:

      • 我实际上测试了各种订阅提供的代码,它总是只返回 Guid { "value": [ { "id": "/tenants/011517f6-bf6d-4a4e-bf42-782a23656699", "tenantId ": "011517f6-bf6d-4a4e-bf42-782a23656699" }, { "id": "/tenants/48012b09-0ee6-4a75-ac27-242a3a466f11", "tenantId": "48012b09-0ee6-4a75-ac217-242a3a46" } ] }
      • 你试过这个线程中提供的代码吗? stackoverflow.com/questions/54655730/…
      • 我试过了,但是端点不一样。我得到了 graph.windows.net 的令牌,而不是 graph.microsoft.com 的令牌。我只是验证 Rohit 的解决方案是否按我的预期工作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 1970-01-01
      • 2016-05-27
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-05
      相关资源
      最近更新 更多