How to create app registration using Azure SDK

2020-07-19 05:18发布

问题:

I need to create an app registration with Azure AD using Azure SDK (or using rest api call, if it's not possible with SDK)

normally you do it manually using portal:

or calling Azure CLI command az ad app create

How can I do it from SDK or REST service

回答1:

There are 2 possible ways to do this. You can pick what works based on your scenario.

  1. Microsoft Graph API Beta Endpoint

    Microsoft Graph API Beta endpoint and working with Application resource (as answered by Jean-Marc Prieur earlier too).

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

    NOTE: This would work but caveat being it's a beta endpoint. So if you're doing this for testing/learning that's fine but if you plan to use it for production application code it would not be recommended.

    See Microsoft Graph beta endpoint documentation itself to see Microsoft's recommendation.

    Also note that since currently this functionality is in Beta, you won't be able to use the Microsoft Graph .NET Client Library, but once it's released for general availability, even Client Library will probably be refreshed to support these operations. See this SO post by Marc LaFleur with similar context.

  2. Azure AD Graph API

    Azure AD Graph API which is an older API and Microsoft Graph API is newer and recommended one for any operations possible. Your case just happens to be one where Microsoft Graph API stable version (v1.0) has not caught up yet and that functionality is only available in beta, hence for production version code you should still use older Azure AD Graph API or it's client library. Read about comparisons and special use cases here

    You can use Azure AD Graph API and Application entity. POST operation can help you create an application.

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

    Read about the details: Application Entity - Azure AD Graph API

    You can choose to call this API directly or make use of Azure AD Graph Client Library

    Here is a quick and dirty 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.

    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 two 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

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


回答2:

You can use the Microsoft Graph API.

The API to use to create an app is: https://developer.microsoft.com/en-us/graph/docs/api-reference/beta/api/application_post_applications and more generally to manipulate apps: https://developer.microsoft.com/en-us/graph/docs/api-reference/beta/resources/application