C# 如何读取Azure Active Directory的域
我创建了新的Azure帐户,并尝试使用以下代码在其中自动部署应用程序:C# 如何读取Azure Active Directory的域,c#,azure-active-directory,C#,Azure Active Directory,我创建了新的Azure帐户,并尝试使用以下代码在其中自动部署应用程序: var app = azure.AccessManagement.ActiveDirectoryApplications .Define(appName) .WithSignOnUrl(appSignOnUrl) .WithAvailableToOtherTenants(true) .WithIdentifierUrl(identifierUrl) .DefinePasswordCredential(username) .W
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来读取它
您似乎只是在尝试读取租户名称。您可以通过调用获取您登录的租户的名称
https://management.azure.com/tenants?$skiptoken={skiptoken}&api-version={api-version}
有关详细信息,请参阅。这将为您提供您授权的所有租户的列表。获取与Azure AD租户关联的域名的代码 请知道,可以有多个域名与您的租户关联。您在屏幕截图中显示的问题只是创建Azure广告时分配给租户的第一个问题,并且在使用.onmicrosoft.com后已经过验证 您可以始终将其他域与Azure AD租户关联,您可以证明其所有权并对其进行验证。稍后我会稍微讨论一下,但首先是相关代码。在您的情况下,您可能只会返回一个默认域 这是我用Azure广告租户快速编写和测试的工作代码。因为您已经在使用fluentapi来创建应用程序,所以应该非常类似 我在一个简单的控制台应用程序中使用了.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();
}
}
}
使用系统;
使用Microsoft.Azure.Management.Fluent;
使用Microsoft.Azure.Management.Graph.RBAC.Fluent;
使用Microsoft.Azure.Management.ResourceManager.Fluent;
名称空间控制台EAPP1
{
班级计划
{
静态void Main(字符串[]参数)
{
//无论您已经使用何种方法进行身份验证(如通过文件、凭据或证书)
//同样也可以用于获取AzureCredentials,如果需要,只需将FromFile更改为FromServicePrincipal即可
IAzure azure=azure.Authenticate(“my.azureauth”).WithDefaultSubscription();
var creds=SdkContext.AzureCredentialsFactory.FromFile(“my.azureauth”);
IGraphRbacManager graphRbacManager=graphRbacManager.Authenticate(creds,“”);
var domains=graphRbacManager.internal.domains.ListAsync().GetAwaiter().GetResult();
string defaultDomain=string.Empty;
foreach(域中的var域)
{
Console.WriteLine(域名);
if(domain.IsDefault.HasValue&&domain.IsDefault.Value==true)
defaultDomain=domain.Name;
//不是故意打破循环,只要打印出多个域名就可以了。
}
string identiferUri=string.Format(“https://{0}/myuniqueapp1”,defaultDomain);
var app=azure.AccessManagement.ActiveDirectoryApplications
.Define(“我唯一的应用程序1”)
.使用SignonUrl(“https://myuniqueapp1.azurewebsites.net")
.其他租户可用(正确)
.WithIdentifierUrl(IdentifierUri)
.DefinePasswordCredential(“字符串”)
.WithPasswordValue(“字符串”)
.WithDuration(新的时间跨度(365,0,0,0))
附(
.CreateAsync();
Console.ReadLine();
}
}
}
为您的Azure广告租户识别身份和与已验证域的关系
使用identifierUrl(identifierUrl)在代码中创建应用程序它进入并将提供的Url添加到应用程序清单的IdentifierURI
集合中。从Azure门户,您将看到在应用程序注册的属性>应用程序ID URI中指定的此值。您还可以编辑清单,并直接在门户中查看它
此值唯一标识您的应用程序。对于单租户应用程序,您可以将其设置为Azure AD中任何其他应用程序都不使用的任何唯一值,但对于多租户应用程序,它必须在全局范围内强制执行,因此存在使用URL的限制,其中主机名与您的一个验证域匹配Azure AD租户。由于您使用的是。WithAvailableToOtherTenants(true)
,因此此概念与您相关
下面是有关Microsoft文档的几个链接,其中讨论了这一点-
AzureCredentials
的服务主体要运行的代码,应具有足够的权限
转到Azure AD>应用程序注册>服务主体的应用程序注册(您可以通过应用程序id找到它,它将具有与服务主体相同的应用程序id)>转到所需权限>添加Windows Azure Active Directory,并为您的代码提供所需的适当应用程序权限
最后,请确保执行“授予权限”,因为此处的所有应用程序权限都需要管理员同意。我实际测试了各种订阅提供的代码,它总是只返回Guid{“value”:[{“id”:“/011517f6-bf6d-4a4e-bf42-782A236569”,“tenantId”:“011517f6-bf6d-4a4e-bf42-782A236569”},{“id”:“/tenants/48012b09-0ee6-4a75-ac27-242a3a466f11”,“tenantId:“48012b09-0ee6-4a75-ac27-242a3a466f11”}您尝试过此线程中提供的代码吗?我尝试过,但最后失败了