【问题标题】:Creating Azure AD application and a service principal using .NET Core (the API and HTTP)使用 .NET Core(API 和 HTTP)创建 Azure AD 应用程序和服务主体
【发布时间】:2017-07-01 16:45:04
【问题描述】:

为了继续以编程方式创建 Azure 应用程序(这从 https://stackoverflow.com/a/44753728/1332416 继续),我的核心获得了一个 subscriptionID 和一个 tenantId,如下所示,但我不知道如何创建本地应用程序及其关联的服务主体。这基本上是在 PowerShell 的情况下使用 New-AzureRmADApplicationNew-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


    【解决方案1】:

    您可以获取访问令牌并调用 Azure AD Graph API 来创建应用程序和服务主体。 (Microsoft Graph API 也允许这样做,但只能通过 beta 端点)

    您可以在此处找到应用程序实体文档:https://msdn.microsoft.com/en-us/library/azure/ad/graph/api/entity-and-complex-type-reference#application-entity

    要获取令牌,您的应用需要在 Azure AD 中为 Azure AD Graph API 授予必要的委派/应用权限。

    那么当你拿到令牌时,使用https://graph.windows.net作为资源。

    那么您应该可以使用这样的正文发布到https://graph.windows.net/tenant-id/applications?api-version=1.6

    {
      "displayName":"Test app",
      "identifierUris": [
        "https://mytenant.onmicrosoft.com/testapp"
      ],
      "homePage":"http://localhost"
    }
    

    将 URL 中的 tenant-id 替换为您的租户 ID。

    这是创建的应用程序,在响应中您将获得创建的应用程序。

    从中获取appId 字段,您将需要它来创建服务主体。

    然后发一个 POST 到:https://graph.windows.net/tenant-id/servicePrincipals?api-version=1.6,正文如下:

    {
      "appId":"eb167a6d-aaaa-aaaa-aaaa-46e981be37fa"
    }
    

    这应该会创建相应的服务主体。

    【讨论】:

    • Microsoft 似乎建议使用较新的 Graph API,例如参见 msdn.microsoft.com/en-us/library/azure/ad/graph/howto/…。让我检查一下...
    • 是的 :) 但是具体功能仍然只在 MS Graph 的 beta 端点上。如果你愿意使用它,你可以使用它来达到同样的效果。
    • 你可能知道,已经快午夜了。我对这些 API 的了解几乎为零,所以我不得不将实验推迟到明天。但我认为测试版是要走的路,我有点担心我已经(几乎)运行的代码(见代码)和 .NET Core 的结果如何,所以尝试一下,看看我是否应该更新问题(也许我应该替换 azure-graph-api.
    • 一会儿,它出现的访问令牌与我在代码 sn-p 中使用的访问令牌相同,我已经无法使用此 API(我得到拒绝访问)。我想我还需要检查登录阶段。
    • 您需要为图形 API 使用不同的令牌。请求另一个,但将资源设置为https://graph.microsoft.com
    猜你喜欢
    • 2020-01-25
    • 2017-07-26
    • 2023-02-22
    • 1970-01-01
    • 1970-01-01
    • 2020-01-31
    • 1970-01-01
    • 2017-08-29
    • 2019-10-17
    相关资源
    最近更新 更多