gpt4 book ai didi

c# - 如何读取 Azure Active Directory 的域

转载 作者:可可西里 更新时间:2023-11-01 09:12:21 24 4
gpt4 key购买 nike

我创建了新的 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。

AD domain value.

最佳答案

获取与您的 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 添加到应用程序 list 的 identifierUris 集合中。在 Azure 门户中,您将看到在应用注册的属性 > 应用 ID URI 中指定的值。您还可以编辑 list 并直接在门户中查看它。

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

这里有几个关于 Microsoft Docs 的链接,其中讨论了这个 -

需要权限

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

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

enter image description here

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

关于c# - 如何读取 Azure Active Directory 的域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54825934/

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