Managed identities - Azure App Service (2024)

  • Article

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

Important

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 recreate the managed identities after such a move, see Will managed identities be recreated automatically if I move a subscription to another directory?. Downstream resources also need to have access policies updated to use the new identity.

Note

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

A managed identity from Microsoft Entra ID allows your app to easily access other Microsoft Entra protected resources such as Azure Key Vault. The identity is managed by the Azure platform and does not require you to provision or rotate any secrets. For more about managed identities in Microsoft Entra ID, see Managed identities for Azure resources.

Your application can be granted two types of identities:

  • A system-assigned identity is tied to your application and is deleted if your app is deleted. An app can only have 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.

The managed identity configuration is specific to the slot. To configure a managed identity for a deployment slot in the portal, navigate 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>.

This video shows you how to use managed identities for App Service.

The steps in the video are also described in the following sections.

Add a system-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 Save.

    Managed identities - Azure App Service (1)

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 config.

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

First, you'll need to create a user-assigned identity resource.

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

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

  3. Select Identity.

  4. Select User assigned > Add.

  5. Search for the identity you created earlier, select it, and select Add.

    Managed identities - Azure App Service (2)

    Once you select Add, the app restarts.

Configure target resource

You may need to configure the target resource to allow access from your app or function. For example, if you request a token to access Key Vault, you must also add an access policy that includes the managed identity of your app or function. Otherwise, your calls to Key Vault will be rejected, even if you use a valid token. The same is true for Azure SQL Database. To learn more about which resources support Microsoft Entra tokens, see Azure services that support Microsoft Entra authentication.

Important

The back-end services for managed identities maintain a cache per resource URI for around 24 hours. If you update the access policy of a particular target resource and immediately retrieve 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 Microsoft Entra ID, such as 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 REST 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
  • .NET
  • JavaScript
  • Python
  • Java
  • PowerShell

A raw HTTP GET request looks like the following example:

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

And a sample response might look like the following:

HTTP/1.1 200 OKContent-Type: application/json{ "access_token": "eyJ0eXAi…", "expires_on": "1586984735", "resource": "https://vault.azure.net", "token_type": "Bearer", "client_id": "5E29463D-71DA-4FE0-8E69-999B57DB23B0"}

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

For more information on the REST endpoint, see REST endpoint reference.

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.

  • 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. Then follow the steps based on the identity type:

    • System-assigned identity: Within the System assigned tab, switch Status to Off. Click Save.
    • User-assigned identity: Select the User assigned tab, select the checkbox for the identity, and select Remove. Select Yes to confirm.

Note

There is also an application setting that can be set, WEBSITE_DISABLE_MSI, which just disables the local token service. However, it leaves the identity in place, and tooling will still 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 by defining two environment variables:

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

The IDENTITY_ENDPOINT 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, including the following parameters:

Parameter nameInDescription
resourceQueryThe Microsoft Entra resource URI of the resource for which a token should be obtained. This could be one of the Azure services that support Microsoft Entra authentication or any other resource URI.
api-versionQueryThe version of the token API to be used. Use 2019-08-01.
X-IDENTITY-HEADERHeaderThe value of the IDENTITY_HEADER environment variable. This header is used to help mitigate server-side request forgery (SSRF) attacks.
client_idQuery(Optional) The client ID of the user-assigned identity to be used. Cannot be used on a request that includes principal_id, msi_res_id, or object_id. If all ID parameters (client_id, principal_id, object_id, and msi_res_id) are omitted, the system-assigned identity is used.
principal_idQuery(Optional) The principal ID of the user-assigned identity to be used. object_id is an alias that may be used instead. Cannot be used on a request that includes client_id, msi_res_id, or object_id. If all ID parameters (client_id, principal_id, object_id, and msi_res_id) are omitted, the system-assigned identity is used.
msi_res_idQuery(Optional) The Azure resource ID of the user-assigned identity to be used. Cannot 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 msi_res_id) are omitted, the system-assigned identity is used.

Important

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 system-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
Managed identities - Azure App Service (2024)

FAQs

What is managed identity in Azure App Service? ›

Managed identities in App Service make your app more secure by eliminating secrets from your app, such as credentials in the connection strings. In this tutorial, you add managed identity to the sample web app you built in one of the following tutorials: Tutorial: Build an ASP.NET app in Azure with Azure SQL Database.

Which Azure services support managed identities? ›

Services supporting managed identities
Service NameDocumentation
Azure SignalR ServiceManaged identities for Azure SignalR Service
Azure Spring AppsEnable system-assigned managed identity for an application in Azure Spring Apps
Azure SQLManaged identities in Microsoft Entra for Azure SQL
53 more rows
Mar 14, 2024

What are the benefits of using managed identities for Azure services? ›

Here are some of the benefits of using managed identities:
  • You don't need to manage credentials. ...
  • You can use managed identities to authenticate to any resource that supports Microsoft Entra authentication, including your own applications.
  • Managed identities can be used at no extra cost.
Oct 23, 2023

How do I create a managed identity for an app service? ›

To set up a managed identity in the portal, you first create an application and then enable the feature.
  1. Access your App Services resource in the Azure portal. ...
  2. Scroll down to the Settings group in the left pane, and select Identity.
  3. On the System assigned tab, switch Status to On and select Save.
Feb 20, 2024

What are the different types of managed identities? ›

There are two types of managed identities: system-assigned and user-assigned. System-assigned managed identities have their lifecycle tied to the resource that created them.

What is the difference between service principal and managed identities? ›

Managed identities are often the preferred choice for Azure resources because they eliminate many of the security risks associated with manually managing credentials. However, service principals offer more flexibility and can be used securely when configured and managed properly.

What is the difference between Azure managed identity and service account? ›

Service Principal and Managed Identity are both tools for Azure identity management. However, their ideal usage differs. Service Principal is great for apps that need specific access and control. Whereas Managed Identity is good when you want Azure to handle the login details automatically.

What is the limitation of managed identity in Azure? ›

Requests to the Managed Identity category are limited to 20 requests per second and 5 concurrent requests. You can read more at the Azure Instance Metadata Service (Windows) article.

How do I create a managed identity in Azure? ›

Add a system-assigned managed identity
  1. Sign in to the Azure portal and go to Azure Deployment Environments.
  2. On Dev centers, select your dev center.
  3. On the left menu under Settings, select Identity.
  4. Under System assigned, set Status to On.
  5. Select Save.
  6. In the Enable system assigned managed identity dialog, select Yes.
Dec 4, 2023

How do I add identity provider to Azure app? ›

In the Azure portal menu, select Resource groups, or search for and select Resource groups from any page. In Resource groups, find and select your resource group. In Overview, select your app's management page. On your app's left menu, select Authentication, and then click Add identity provider.

What is the difference between system assigned and user assigned managed identities? ›

System-assigned identities are automatically deleted when the resource is deleted, while the lifecycle of a user-assigned identity is independent of any resources with which it's associated.

How do Azure managed identities work? ›

How do managed identities work? Through managed identities, you can request access tokens for the resources that support Azure AD authentication. Access tokens are received based on the RBAC assigned to them on the resource. Once the resource receives the access token, it can be accessed..

How secure are Azure managed identities? ›

In conclusion, while Azure Managed Identities offer a secure and efficient way to manage resource access, they can also pose security risks if not properly managed.

Which Azure service provides the best identity as a service experience? ›

Microsoft Entra ID is the Azure solution for identity and access management. Microsoft Entra ID is a multitenant, cloud-based directory and identity management service from Microsoft.

What is managed identity used for? ›

Managed identities can be used to request and receive Azure AD tokens without any requirement to govern credentials, secrets, keys, and passwords.

What is Azure service principal vs managed identity? ›

Managed identities are often the preferred choice for Azure resources because they eliminate many of the security risks associated with manually managing credentials. However, service principals offer more flexibility and can be used securely when configured and managed properly.

What is the difference between managed identity and Azure AD? ›

A managed identity is a feature of Azure Active Directory (Azure AD) that automatically manages the creation, rotation, and revocation of credentials used to access resources. You don't need to provide a client secret for getting the credential from AD.

Top Articles
Latest Posts
Article information

Author: Rubie Ullrich

Last Updated:

Views: 5716

Rating: 4.1 / 5 (72 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Rubie Ullrich

Birthday: 1998-02-02

Address: 743 Stoltenberg Center, Genovevaville, NJ 59925-3119

Phone: +2202978377583

Job: Administration Engineer

Hobby: Surfing, Sailing, Listening to music, Web surfing, Kitesurfing, Geocaching, Backpacking

Introduction: My name is Rubie Ullrich, I am a enthusiastic, perfect, tender, vivacious, talented, famous, delightful person who loves writing and wants to share my knowledge and understanding with you.