Programmatically register app in azure active directory using graph api

后端 未结 1 1203
别那么骄傲
别那么骄傲 2021-01-20 06:02

I am trying to register an application in Azure AD using graph API, I have a method CallRestAPI which will make the request. Below is the code

          


        
相关标签:
1条回答
  • 2021-01-20 06:25

    A better way to achieve the same i.e. register an app with Azure AD will be to make use of Azure AD Graph Client Library

    I say it's a better approach because when you use the client library you reap multiple benefits like no raw HTTP request handling, writing more convenient and declarative C# code, depending on a well tested library, async support etc.

    Underlying Graph API used will still be the same I suppose

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

    Here is sample code (C#) to create an Azure AD application

    Notice that I've kept app.PublicClient flag as true to register as a native application. You can set it to false if you want to register it as a web application.

            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;
    
                }
              }
            }
    

    Setup: I have an application registered in Azure AD, which has required permissions as application permission - Read and Write all applications and grant permissions is done for this app. Now using this application's client id and client secret, a token is acquired and Azure AD Graph API is called to create an application. It is not mandatory to use application permissions, you can also use delegated permissions by prompting user for credentials. See links to more detailed examples (old ones but still useful).

    • Console Application using Graph client library

    • Web app calls Graph using Graph client library

    • Azure AD Graph Client Library 2.0 Announcement page

    On a side note, you could do this using the newer Microsoft Graph API as well,

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

    but the ability to create applications is still in beta and hence not recommeded for production workloads. So even though Microsoft Graph API would be recommende for most scenarios, at least for this one, using Azure AD Graph API is the way to go currently.

    I have covered this in a little more detail in a similar SO Post here.

    0 讨论(0)
提交回复
热议问题