使用 API 在 Azure AD 中注册应用程序(应用程序注册)?

Register Application in Azure AD(App registrations) Using API?

我需要在 .net core 2.0 中使用 API 在 Azure AD 中注册应用程序(应用程序注册)。 我尝试使用 power-shell 注册应用程序。这是工作。但我需要使用 API 来做到这一点,因为当我们将 power-shell 脚本部署到 Web 服务器上时,它会出现授权问题。

有没有API可以用来注册API?

我试图找到 Microsoft Graph API,但没有找到任何示例。我也试图找到示例,但他们正在使用 power-shell 脚本来注册应用程序。

根据您的要求,您可以使用 microsoft graph api POST https://graph.microsoft.com/v1.0/applications.

要使用此api,您需要完成授权。由于您担心授权,我建议您使用 client_credential grant flow 来完成。可以参考图api文档中下面的代码通过sdk请求api

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var application = new Application
{
    DisplayName = "Display name"
};

await graphClient.Applications
    .Request()
    .AddAsync(application);

对了,你可以参考这个document来了解如何得到上面代码中的authProvider

===================================更新= ==============================

您需要为您创建的应用程序添加权限。我们可以发现 graph api 需要如下权限:

所以我们需要为您创建的应用添加至少一种权限,请参考以下步骤:

添加权限后,不要忘记授予管理员同意。

之后,运行 你的代码。它将创建新的应用程序成功。

=======================更新添加权限======== ===========

要创建具有某些权限的应用程序,您可以使用以下代码:

using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace ConsoleApp28
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
            .Create("<client id>")
            .WithTenantId("<tenant id>")
            .WithClientSecret("<client secret>")
            .Build();

            ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);

            GraphServiceClient graphClient = new GraphServiceClient(authProvider);
            
            var application = new Application
            {
                DisplayName = "huryNewappWithPermissions",
                RequiredResourceAccess = new List<RequiredResourceAccess>()
                {
                    new RequiredResourceAccess
                    {
                        ResourceAppId = "00000003-0000-0000-c000-000000000000",
                        ResourceAccess = new List<ResourceAccess>()
                        {
                            new ResourceAccess
                            {
                                Id = Guid.Parse("e1fe6dd8-ba31-4d61-89e7-88639da4683d"), //id of User.Read(Delegated) permission
                                Type = "Scope"
                            },
                            new ResourceAccess
                            {
                                Id = Guid.Parse("1bfefb4e-e0b5-418b-a88f-73c46d2cc8e9"), //id of Application.ReadWrite.All(Application) permission
                                Type = "Role"
                            }
                        }
                    }
                }
            };

            await graphClient.Applications.Request().AddAsync(application);
        }
    }
}

您可以通过此 api:

列出所有图表权限
https://graph.microsoft.com/v1.0/serviceprincipals?$filter=appId eq '00000003-0000-0000-c000-000000000000'

然后找到权限的id,把它放到上面的代码中。 Type = "Scope"表示权限为“委托”类型,Type = "Role"表示权限为“应用”类型。