获取与 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 和为您的代码提供适当的应用程序权限。
最后,请务必执行“授予权限”,因为此处的所有应用程序权限都需要管理员同意。