Rediger

Del via


Use managed identities for App Service and Azure Functions

Note

Starting June 1, 2024, newly created App Service apps can generate a unique default host name that uses the naming convention <app-name>-<random-hash>.<region>.azurewebsites.net. For example: myapp-ds27dh7271aah175.westus-01.azurewebsites.net. Existing app names remain unchanged.

For more information, see the blog post about creating a web app with a unique default host name.

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

A managed identity from Microsoft Entra ID allows your app to easily access other Microsoft Entra-protected resources, such as Azure Key Vault. The Azure platform manages the identity, so you don't need to provision or rotate any secrets. For more information about managed identities in Microsoft Entra ID, see Managed identities for Azure resources.

You can grant two types of identities to your application:

  • A system-assigned identity is tied to the app and is deleted if the app is deleted. An app can have only one system-assigned identity.
  • A user-assigned identity is a standalone Azure resource that can be assigned to your app. An app can have multiple user-assigned identities. One user-assigned identity can be assigned to multiple Azure resources, such as two App Service apps.

The managed identity configuration is specific to the slot. To configure a managed identity for a deployment slot in the portal, go to the slot first. To find the managed identity for your web app or deployment slot in your Microsoft Entra tenant from the Azure portal, search for it directly from the Overview page of your tenant. Usually, the slot name is similar to <app-name>/slots/<slot-name>.

The following video complements the steps in this article by illustrating how to use managed identities for App Service.

Note

Managed identities are not available for apps deployed in Azure Arc.

Because managed identities don't support cross-directory scenarios, they won't behave as expected if your app is migrated across subscriptions or tenants. To re-create the managed identities after such a move, see Will managed identities be re-created automatically if I move a subscription to another directory?. Downstream resources also need to have access policies updated to use the new identity.

Prerequisites

To perform the steps in this article, you must have a minimum set of permissions over your Azure resources. The specific permissions that you need vary based on your scenario. The following table summarizes the most common scenarios:

Scenario Required permission Example built-in roles
Create a system-assigned identity Microsoft.Web/sites/write over the app, or Microsoft.Web/sites/slots/write over the slot Website Contributor
Create a user-assigned identity Microsoft.ManagedIdentity/userAssignedIdentities/write over the resource group in which the identity will be created Managed Identity Contributor
Assign a user-assigned identity to your app Microsoft.Web/sites/write over the app, Microsoft.Web/sites/slots/write over the slot, or
Microsoft.ManagedIdentity/userAssignedIdentities/*/assign/action over the identity
Website Contributor and Managed Identity Operator
Create Azure role assignments Microsoft.Authorization/roleAssignments/write over the target resource scope Role Based Access Control Administrator or User Access Administrator

Add a system-assigned identity

To enable a system-assigned managed identity, use the following instructions.

  1. In the Azure portal, go to your app's page.

  2. On the left menu, under Settings, select Identity.

  3. On the System assigned tab, switch Status to On. Then select Save.

Screenshot that shows selections for turning on a system-assigned managed identity in the portal.

Add a user-assigned identity

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

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

  2. On the left menu for your app's page, under Settings, select Identity.

  3. Select User assigned > Add.

  4. Search for the identity that you created earlier, select it, and then select Add.

Screenshot that shows selections for adding a user-assigned managed identity in the portal.

After you finish these steps, the app restarts.

Configure the target resource

You need to configure the target resource to allow access from your app. For most Azure services, you configure the target resource by creating a role assignment.

Some services use mechanisms other than Azure role-based access control (RBAC). To understand how to configure access by using an identity, refer to the documentation for each target resource. To learn more about which resources support Microsoft Entra tokens, see Azure services that support Microsoft Entra authentication.

For example, if you request a token to access a secret in Azure Key Vault, you must also create a role assignment that allows the managed identity to work with secrets in the target vault. Otherwise, your calls to Key Vault will be rejected, even if you use a valid token. The same is true for Azure SQL Database and other services.

Important

The back-end services for managed identities maintain a cache per resource URI for around 24 hours. It can take several hours for changes to a managed identity's group or role membership to take effect. It's currently not possible to force a managed identity's token to be refreshed before its expiration. If you change a managed identity's group or role membership to add or remove permissions, you might need to wait several hours for the Azure resource that's using the identity to have the correct access.

For alternatives to groups or role memberships, see Limitation of using managed identities for authorization.

Connect to Azure services in app code

With its managed identity, an app can obtain tokens for Azure resources that Microsoft Entra ID helps protect, such as Azure SQL Database, Azure Key Vault, and Azure Storage. These tokens represent the application that accesses the resource, and not any specific user of the application.

App Service and Azure Functions provide an internally accessible REST endpoint for token retrieval. You can access the REST endpoint from within the app by using a standard HTTP GET request. You can implement the request 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.

A raw HTTP GET request uses the two supplied environment variables and looks like the following example:

GET /MSI/token?resource=https://vault.azure.net&api-version=2019-08-01 HTTP/1.1
Host: <ip-address-:-port-in-IDENTITY_ENDPOINT>
X-IDENTITY-HEADER: <value-of-IDENTITY_HEADER>

A sample response might look like the following example:

HTTP/1.1 200 OK
Content-Type: application/json

{
    "access_token": "eyJ0eXAi…",
    "expires_on": "1586984735",
    "resource": "https://vault.azure.net",
    "token_type": "Bearer",
    "client_id": "00001111-aaaa-2222-bbbb-3333cccc4444"
}

This response is the same as the response for the Microsoft Entra service-to-service access token request. To access Key Vault, you add the value of access_token to a client connection with the vault.

For more information on the REST endpoint, see REST endpoint reference later in this article.

Remove an identity

When you remove a system-assigned identity, it's deleted from Microsoft Entra ID. System-assigned identities are also automatically removed from Microsoft Entra ID when you delete the app resource itself.

  1. On the left menu for your app's page, under Settings, select Identity.

  2. Follow the steps based on the identity type:

    • For a system-assigned identity: On the System assigned tab, switch Status to Off. Then select Save.
    • For a user-assigned identity: Select the User assigned tab, select the checkbox for the identity, and then select Remove. Select Yes to confirm.

Note

You can also set an application setting that disables only the local token service: WEBSITE_DISABLE_MSI. However, it leaves the identity in place. Tooling still shows the managed identity as on or enabled. As a result, we don't recommend that you use this setting.

REST endpoint reference

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

  • IDENTITY_ENDPOINT: The URL to the local token service.
  • IDENTITY_HEADER: A header that can help mitigate server-side request forgery (SSRF) attacks. The platform rotates the value.

The IDENTITY_ENDPOINT variable is a local URL from which your app can request tokens. To get a token for a resource, make an HTTP GET request to this endpoint. Include the following parameters:

Parameter name In Description
resource Query The Microsoft Entra resource URI of the resource for which a token should be obtained. This resource could be one of the Azure services that support Microsoft Entra authentication or any other resource URI.
api-version Query The version of the token API to be used. Use 2019-08-01.
X-IDENTITY-HEADER Header The value of the IDENTITY_HEADER environment variable. This header is used to help mitigate SSRF attacks.
client_id Query (Optional) The client ID of the user-assigned identity to be used. It can't 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. The object_id parameter is an alias that can be used instead. It can't 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 resource ID of the user-assigned identity to be used. It can't be used on a request 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.

Important

If you're trying to obtain tokens for user-assigned identities, you must include one of the optional properties. Otherwise, the token service will try to obtain a token for a system-assigned identity, which might or might not exist.