![](/img/trans.png)
[英]How to use Active Directory connection string membership provider when it is published outside our domain or on azure
[英]How to read domain of Azure Active Directory
我创建了新的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。
看来您只是在尝试读取租户名称。 您可以通过致电获取登录用户的姓名
https://management.azure.com/tenants?$skiptoken={skiptoken}&api-version={api-version}
有关详情,请参见此页面 。 这将为您提供您授权的所有租户的列表。
获取与您的Azure AD租户关联的域名的代码
请注意,您的租户可以有多个域名。 您在屏幕快照中显示的有关您的问题的只是第一个,在创建Azure AD时已分配给您的租户,并且由于已使用.onmicrosoft.com而已通过验证。 链接
您始终可以将其他域与Azure AD租户相关联,以证明其所有权并进行验证。 稍后我会对此稍作介绍,但首先是相关代码。 在您的情况下,您可能只会获得一个默认域。
我正在使用Azure AD租户快速编写并测试此工作代码。 由于您已经在使用流畅的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时存在限制名称与您的Azure AD租户的已验证域之一匹配。 由于您使用的是.WithAvailableToOtherTenants(true)
因此此概念与您相关。
这是Microsoft Docs上有关此问题的几个链接-
所需权限
希望您已经完成了此步骤,因为您需要创建应用程序的权限,但是如果您以后不需要或其他任何人阅读,因为代码正在从Azure AD中读取信息并在Azure AD中创建新应用程序,用于获取运行该代码的AzureCredentials
的服务主体应具有足够的特权。
转到您的Azure AD>应用程序注册>服务主体的应用程序注册(您可以通过应用程序ID查找它,它将与您的服务主体具有相同的应用程序ID)>转到必需的权限>添加Windows Azure Active Directory并提供适当的应用程序您的代码所需的权限。
最后,请确保执行“授予权限”,因为此处的所有应用程序权限都需要管理员同意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.