使用图形API中天青活性目录编程寄存器应用

问题描述 投票:3回答:1

我试图注册使用图形API中天青AD的应用程序,我有一个方法CallRestAPI这将使该请求。下面是代码

    public async Task<Response> AzureADApp()
    {
        Response responseMessage = new Response();
        try
        {
            var token = GenerateToken();
            List<(string, string)> listHeaders = new List<(string, string)>();
            listHeaders.Add(("Authorization", string.Concat("Bearer" + " " + token)));
            listHeaders.Add(("Content-Type", "application/json"));

            List<(string, string)> param = new List<(string, string)>();
            param.Add(("displayName", "VS1Application123"));
            param.Add(("homepage", "https://localhost:44358/"));
            param.Add(("identifierUris", "https://G7CRM4L/6958490c-21ae-4885-804c-f03b3add87ad"));

            string callUrl = "https://graph.windows.net/G7CRM4L/applications/?api-version=1.6";
            var result = CallRestAPI(callUrl, "", Method.POST, listHeaders, param);

        }
        catch (Exception ex)
        {
            responseMessage.StatusCode = Convert.ToInt16(HttpStatusCode.InternalServerError);
        }
        return responseMessage;
    }

    public async Task<IRestResponse> CallRestAPI(string BaseAddress, string SubAddress, Method method, List<(string, string)> headersList = null, List<(string, string)> paramsList = null)
    {
        var call = new RestClient(BaseAddress + SubAddress);
        var request = new RestRequest(method);

        if (headersList != null)
        {
            foreach (var header in headersList)
            {
                request.AddHeader(header.Item1, header.Item2);
            }
        }
        if (paramsList != null)
        {
            foreach (var param in paramsList)
            {
                request.AddParameter(param.Item1, param.Item2);
            }
        }

        var response = call.ExecuteTaskAsync(request);

        return response.Result;
    }

我想我的身体发送参数的方法是不正确的任何人都可以指导我如何使此代码的工作还是有更好的方法来达到同样的?谢谢。

c# azure-active-directory microsoft-graph azure-ad-graph-api
1个回答
1
投票

一种更好的方式来实现相同的寄存器即天青AD一个应用程序将是使利用Azure AD Graph Client Library

我说这是一个更好的办法,因为当你使用客户端库你收获一样,没有原始的HTTP请求处理多重效益,书写更加方便和声明的C#代码,这取决于一个很好的测试库,异步支持等。

使用仍将是我想相同的底层图形API

POST https://graph.windows.net/{tenant-id}/applications?api-version=1.6

下面是示例代码(C#)来创建一个天青AD应用

请注意,我已经把app.PublicClient标志作为真正的原生应用程序注册。如果你想将其注册为一个Web应用程序可以将其设置为false。

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        using Microsoft.Azure.ActiveDirectory.GraphClient;
        using Microsoft.IdentityModel.Clients.ActiveDirectory;

        namespace CreateAzureADApplication
        {
            class Program
            {
                static void Main(string[] args)
                {

                    ActiveDirectoryClient directoryClient;

                    ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(new Uri("https://graph.windows.net/{yourAADGUID}"),
            async () => await GetTokenForApplication());


                    Application app = new Application();
                    app.DisplayName = "My Azure AD Native App";
                    app.PublicClient = true;
                    app.Homepage = "https://myazureadnativeapp";
                    activeDirectoryClient.Applications.AddApplicationAsync(app).GetAwaiter().GetResult();

                 }

             public static async Task<string> GetTokenForApplication()
             {
                   AuthenticationContext authenticationContext = new AuthenticationContext(
                "https://login.microsoftonline.com/{yourAADGUID}",
                false);

            // Configuration for OAuth client credentials 

                ClientCredential clientCred = new ClientCredential("yourappclientId",
                    "yourappclientsecret"
                    );
                AuthenticationResult authenticationResult =
                    await authenticationContext.AcquireTokenAsync("https://graph.windows.net", clientCred);

                return authenticationResult.AccessToken;

            }
          }
        }

设置:我在Azure的AD注册的应用程序,它已要求权限应用程序的权限 - 读写所有的应用程序和授予权限为这个应用程序来完成。现在,使用此应用程序的客户端ID和客户端密钥,令牌获取和Azure的AD图形API调用来创建一个应用程序。它不是强制使用应用程序的权限,还可以通过提示用户凭据使用委托权限。查看指向更具体的例子(旧的但仍然有用)。

在一个侧面说明,你可以这样做使用较新的Microsoft Graph API为好,

    POST https://graph.microsoft.com/beta/applications

但创建应用程序的功能仍处于测试阶段,因此没有特别建议用于生产工作负载。因此,即使微软图形API将recommende对于大多数情况,这一个,使用Azure的AD图形API至少是目前走的路。

我曾经在一个更详细一点类似的SO Post这里讨论这个。

© www.soinside.com 2019 - 2024. All rights reserved.