AzureRecipes

Introduction

Theory

In the Azure ecosystem the access to API’s is often managed with Active Directory objects and assigned permissions. These API’s may be custom endpoints or endpoints for a bunch of Microsoft applications such as DevOps, Dynamics 365, Power Platform applications, OneNote … but in most situations just the Microsoft Graph which is the consolidated access to almost any data. To manage permissions for those API’s, there are three different identity representations in the AAD:

The access to an API can be done in two different ways (which is important to understand):

App Services and Functions has two built-in capabilities to generate an authentication / authorization context which can be used as basis to establish access to API’s:

Recommendations

Approach Delegated (“on-behalf-of”) Application (“background-job”) Recommendations
Managed Identity Not Feasible Supported Use it for connections which do not rely on configured API permissions, but role assignment based on AAD identity. Mostly for access to other Azure Services (e.g. Key Vault or Databases). Use user-assigned identities only if the reuse of an identity is beneficial (e.g. load balancing scenarios) and the clean-up is ensured.
Built-In Auth Favoured Supported Use it for delegated scenarios whenever possible and elaborate proper configuration of unauthenticated access. Use it for application-based access if it based on API permissions (such as with Microsoft Graph), especially if you have both delegated and application-based access to resources.

Getting Started

Create an App Registration and assign in to Function (you may take azuredeploy.json as a basis). Create an additional secret on it and copy the according settings to local.settings.json (avoid to commit sensitive data to the source management).

Note 1: The provided sample functions require the API permission Organisation.Read.All for type application. Adjust permissions or called API method depending on your needs.

Note 2: This snippet works without any adjustments for both, regular Active Directory (AAD) as well as AAD B2C. Just specify the values for ClientId, TenantId and Secret for deployment of ARM template or in local.settings.json.

Code

The snippet provides do slightly different solutions represented by the two functions in the Function App. Both also work locally on a deployment machine without any special measures other than the configuration.

Deployment (Azure Resources)

Deploy to Azure

Further Reading

Built-In AuthN / AuthZ

Alternative Approach using Managed Identities

Generate Access Token for delegated / on-behalf-of Access