【发布时间】:2017-07-01 16:45:04
【问题描述】:
为了继续以编程方式创建 Azure 应用程序(这从 https://stackoverflow.com/a/44753728/1332416 继续),我的核心获得了一个 subscriptionID 和一个 tenantId,如下所示,但我不知道如何创建本地应用程序及其关联的服务主体。这基本上是在 PowerShell 的情况下使用 New-AzureRmADApplication 和 New-AzureRmADServicePrincipal 的地方。这个问题在 https://stackoverflow.com/a/44631758/1332416 得到了部分回答,但看起来 .NET Core 可能是这里一些问题的原因,因为找不到类型。
更具体地说,我有如下代码
string resource = "https://management.core.windows.net/";
string clientId = "1950a258-227b-4e31-a9cf-717495945fc2";
string userName = "<replace>";
string password = "<replace>";
string apiVersion = "2016-06-01";
using(var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string tokenEndpoint = "https://login.microsoftonline.com/common/oauth2/token";
var body = $"resource={resource}&client_id={clientId}&grant_type=password&username={userName}&password={password}";
var stringContent = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded");
var response = await client.PostAsync(tokenEndpoint, stringContent).ConfigureAwait(false);
var result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
JObject jobject = JObject.Parse(result);
var token = jobject["access_token"].Value<string>();
client.DefaultRequestHeaders.Add("Authorization", $"bearer {token}");
var subcriptions = await client.GetStringAsync($"https://management.azure.com/subscriptions?api-version={apiVersion}").ConfigureAwait(false);
var tenants = await client.GetStringAsync($"https://management.azure.com/tenants?api-version={apiVersion}").ConfigureAwait(false);
Console.WriteLine(subcriptions);
Console.WriteLine(tenants);
//We have the SubscriptionID, TenantId and the token to
//the subscription here. How to do New-AzureRmADApplication and New-AzureRmADServicePrincipal with the application ID here? Specifically I'm considering to use a certificate thumbrint if it matters.
//var subscription = "... from subscriptions...";
//var tenantId = "... from tenants...";
}
【问题讨论】:
标签: azure .net-core azure-active-directory microsoft-graph-api azure-sdk-.net