How to utilise managed identities for App Service and Azure Functions

This commodity shows you how to create a managed identity for App Service and Azure Functions applications and how to utilize it to access other resources.

Of import

Managed identities for App Service and Azure Functions won't behave as expected if your app is migrated across subscriptions/tenants. The app needs to obtain a new identity, which is done by disabling and re-enabling the feature. Downstream resources also need to have access policies updated to use the new identity.

A managed identity from Azure Active Directory (Azure Advertising) allows your app to easily admission other Azure Advertizement-protected resource such as Azure Key Vault. The identity is managed by the Azure platform and does non require you to provision or rotate whatsoever secrets. For more about managed identities in Azure Advertizing, run into Managed identities for Azure resources.

Your application tin be granted two types of identities:

  • A system-assigned identity is tied to your awarding and is deleted if your app is deleted. An app can only have one arrangement-assigned identity.
  • A user-assigned identity is a standalone Azure resource that can be assigned to your app. An app tin accept multiple user-assigned identities.

Add together a organisation-assigned identity

  • Azure portal
  • Azure CLI
  • Azure PowerShell
  • ARM template
  1. In the left navigation of your app's page, scroll down to the Settings group.

  2. Select Identity.

  3. Within the System assigned tab, switch Status to On. Click Salve.

    Screenshot that shows where to switch Status to On and then select Save.

Note

To observe the managed identity for your web app or slot app in the Azure portal, under Enterprise applications, expect in the User settings section. Unremarkably, the slot proper name is similar to <app name>/slots/<slot name>.

Add a user-assigned identity

Creating an app with a user-assigned identity requires that you create the identity and so add together its resource identifier to your app config.

  • Azure portal
  • Azure CLI
  • Azure PowerShell
  • ARM template

Start, you lot'll need to create a user-assigned identity resources.

  1. Create a user-assigned managed identity resources according to these instructions.

  2. In the left navigation for your app's page, scroll down to the Settings group.

  3. Select Identity.

  4. Inside the User assigned tab, click Add together.

  5. Search for the identity you created before and select it. Click Add.

    Managed identity in App Service

Important

If y'all select Add after you select a user-assigned identity to add, your application will restart.

Configure target resources

You lot may need to configure the target resources to permit access from your app or function. For example, if y'all request a token to access Key Vault, you must as well add an access policy that includes the managed identity of your app or function. Otherwise, your calls to Key Vault volition be rejected, even if y'all use a valid token. The same is true for Azure SQL Database. To learn more about which resources support Azure Agile Directory tokens, run across Azure services that support Azure AD hallmark.

Important

The back-stop services for managed identities maintain a cache per resources URI for around 24 hours. If you update the admission policy of a particular target resource and immediately call back a token for that resource, you may continue to get a cached token with outdated permissions until that token expires. There's currently no way to force a token refresh.

Connect to Azure services in app code

With its managed identity, an app can obtain tokens for Azure resources that are protected by Azure Agile Directory, such equally Azure SQL Database, Azure Key Vault, and Azure Storage. These tokens represent the application accessing the resource, and not any specific user of the application.

App Service and Azure Functions provide an internally accessible Residue endpoint for token retrieval. The REST endpoint can be accessed from within the app with a standard HTTP GET, which can be implemented with a generic HTTP client in every language. For .NET, JavaScript, Java, and Python, the Azure Identity client library provides an abstraction over this REST endpoint and simplifies the development experience. Connecting to other Azure services is as simple as adding a credential object to the service-specific client.

  • HTTP GET
  • .Internet
  • JavaScript
  • Python
  • Java
  • PowerShell

A raw HTTP GET request looks like the following example:

                  Get /MSI/token?resources=https://vault.azure.net&api-version=2019-08-01 HTTP/one.1 Host: localhost:4141 X-IDENTITY-HEADER: 853b9a84-5bfa-4b22-a3f3-0b9a43d9ad8a                                  

And a sample response might wait like the following:

                  HTTP/one.1 200 OK Content-Type: awarding/json  {     "access_token": "eyJ0eXAi…",     "expires_on": "1586984735",     "resources": "https://vault.azure.net",     "token_type": "Bearer",     "client_id": "5E29463D-71DA-4FE0-8E69-999B57DB23B0" }                                  

This response is the same every bit the response for the Azure AD service-to-service admission token request. To admission Key Vault, you will then add together the value of access_token to a client connection with the vault.

For more information on the Rest endpoint, see Residue endpoint reference.

Remove an identity

When y'all remove a organisation-assigned identity, information technology'south deleted from Azure Active Directory. System-assigned identities are too automatically removed from Azure Active Directory when yous delete the app resources itself.

  • Azure portal
  • Azure CLI
  • Azure PowerShell
  • ARM template
  1. In the left navigation of your app'south page, gyre downward to the Settings grouping.

  2. Select Identity. So follow the steps based on the identity type:

    • System-assigned identity: Within the Organisation assigned tab, switch Status to Off. Click Salvage.
    • User-assigned identity: Click the User assigned tab, select the checkbox for the identity, and click Remove. Click Yes to ostend.

Note

There is also an application setting that can exist set, WEBSITE_DISABLE_MSI, which simply disables the local token service. Notwithstanding, information technology leaves the identity in place, and tooling will all the same show the managed identity as "on" or "enabled." As a result, use of this setting is not recommended.

REST endpoint reference

An app with a managed identity makes this endpoint available past defining two environment variables:

  • IDENTITY_ENDPOINT - the URL to the local token service.
  • IDENTITY_HEADER - a header used to assist mitigate server-side asking forgery (SSRF) attacks. The value is rotated past the platform.

The IDENTITY_ENDPOINT is a local URL from which your app tin can request tokens. To get a token for a resource, brand an HTTP Go request to this endpoint, including the following parameters:

Parameter name In Clarification
resources Query The Azure Advertisement resource URI of the resource for which a token should be obtained. This could be one of the Azure services that back up Azure Advertisement authentication or any other resource URI.
api-version Query The version of the token API to exist used. Use "2019-08-01" or later.
10-IDENTITY-HEADER Header The value of the IDENTITY_HEADER environment variable. This header is used to assist mitigate server-side request forgery (SSRF) attacks.
client_id Query (Optional) The client ID of the user-assigned identity to be used. Cannot be used on a request that includes principal_id, mi_res_id, or object_id. If all ID parameters (client_id, principal_id, object_id, and mi_res_id) are omitted, the system-assigned identity is used.
principal_id Query (Optional) The principal ID of the user-assigned identity to be used. object_id is an alias that may exist used instead. Cannot be used on a request that includes client_id, mi_res_id, or object_id. If all ID parameters (client_id, principal_id, object_id, and mi_res_id) are omitted, the system-assigned identity is used.
mi_res_id Query (Optional) The Azure resources ID of the user-assigned identity to be used. Cannot be used on a asking that includes principal_id, client_id, or object_id. If all ID parameters (client_id, principal_id, object_id, and mi_res_id) are omitted, the system-assigned identity is used.

Of import

If you are attempting to obtain tokens for user-assigned identities, you must include one of the optional properties. Otherwise the token service will attempt to obtain a token for a arrangement-assigned identity, which may or may not exist.

Next steps

  • Tutorial: Connect to SQL Database from App Service without secrets using a managed identity
  • Access Azure Storage securely using a managed identity
  • Call Microsoft Graph securely using a managed identity
  • Connect securely to services with Key Vault secrets