Managed Identity for Azure App Services (2024)

Azure App Services supports an interesting feature called Manage Identity from Azure Active Directory.
This allows your App Services to easily connect to Azure Resources such as Azure KeyVault, Azure Storage, Azure SQL . The complete list of resources that support this feature are available in the following document:
Azure Services that support managed identities - Azure AD | Microsoft Docs

You could refer to our documentation for more details on this featurehere.

MSI-Validator helps you troubleshoot issues with Managed Identity for Azure App Services.
The link to download this tool is available in the attachments section of the blog.

Installation Steps:

  1. Download the zip file from the attachments.
    Current version - v1.0.0.0
  2. Extract it to the local folder.
  3. Drag and drop "msi-validator.exe" to the Kudu console of the App Service (https://<webapp-name>.scm.azurewebsites.net)Managed Identity for Azure App Services (1)

Commands:

  1. To Get an Access Token :
    msi-validator get-token -r <resource>​Valid Arguments for resource = keyvault, storage, sqlSample:msi-validator get-token -r keyvaultmsi-validator get-token -r storagemsi-validator get-token -r sql
  2. To Test the connection
    msi-validator test-connection -r <resource> -e <endpoint>Valid Arguments for resource = keyvault, storage, sqlSample:msi-validator test-connection -r "keyvault" -e "https://<keyvault-name>.vault.azure.net/secrets/<secret-key>"msi-validator.exe test-connection -r storage -e https://<storage-name>.blob.core.windows.net/<container-name>/<blob-path>msi-validator.exe test-connection -r sql -e "Data Source=<server-name>.database.windows.net;Initial Catalog=<database-name>;"​

Troubleshooting:

  1. From the Identity Blade of the App Service, ensure that Managed Identity is turned on.
    Managed Identity for Azure App Services (2)
  2. Navigate to Kudu Console (https://<webapp-name>.scm.azurewebsites.net) > Environment Section and search for MSI (Ctrl + F)
    Managed Identity for Azure App Services (3)

    The Environmental Variables "MSI_ENDPOINT" and "MSI_SECRET" would have been set automatically.

  3. Run the command "msi-validator get-token -r <resource>" and check if a token is being returned.
    Managed Identity for Azure App Services (4)
    An access token should be returned.
    Otherwise, it indicates that MSI service has issues reaching out to Azure Active Directory to fetch a token.


    Things to check :

    • Does the App Service have regional VNet Integration / is the App in ASE?
      Are there any User Defined Routes on the subnet to which the App Service is integrated ?
      If Yes , is the device to which the traffic is force tunneled, blocking any Azure Active Directory Dependency ?
    • Do you still face the issue if Managed Identity is disabled and enabled again ?
  4. Run the command "msi-validator test-connection -r <resource> -e "<endpoint>" " and check if data is returned from the resource or inspect the error message..

    KeyVault:
    From the below error message, we see that the App Service doesn’t have necessary permissions to access the KeyVault.
    Managed Identity for Azure App Services (5)
    Resolution:
  1. Navigate to the Access Policies Blade of KeyVault from the Azure Portal.
  2. Click on "+ Add Access Policy"
    Managed Identity for Azure App Services (6)
  3. Provide the necessary permission.
    Managed Identity for Azure App Services (7)
  4. Choose the Service Principal (name of the App Service)
    Managed Identity for Azure App Services (8)

Storage:

Managed Identity for Azure App Services (9)

Resolution :
Navigate to the Access Control IAM) > Add Role assignment and choose the necessary storage related permission. The roles should be configured as per your application's use case.
Managed Identity for Azure App Services (10)

SQL:
The application could fail while connecting to Azure SQL using MSI with the error message: "Unable to connect to SQL. Exception : Login failed for user '<token-identifiedprincipal>'"

Managed Identity for Azure App Services (11)

Resolution:
If you want, you can add the identity to anAzure AD group, then grant SQL Database access to the Azure AD group instead of the identity. For example, the following commands add the managed identity from the previous step to a new group calledmyAzureSQLDBAccessGroup:

groupid=$(az ad group create --display-name myAzureSQLDBAccessGroup --mail-nickname myAzureSQLDBAccessGroup --query objectId --output tsv)msiobjectid=$(az webapp identity show --resource-group myResourceGroup --name <app-name> --query principalId --output tsv)az ad group member add --group $groupid --member-id $msiobjectidaz ad group member list -g $groupid

In the Cloud Shell, sign in to SQL Database by using the SQLCMD command. Replace<server-name>with your server name,<db-name>with the database name your app uses, and<aad-user-name>and<aad-password>with your Azure AD user's credentials.

sqlcmd -S <server-name>.database.windows.net -d <db-name> -U <aad-user-name> -P "<aad-password>" -G -l 30

In the SQL prompt for the database you want, run the following commands to grant the permissions your app needs. For example,

CREATE USER [<identity-name>] FROM EXTERNAL PROVIDER;ALTER ROLE db_datareader ADD MEMBER [<identity-name>];ALTER ROLE db_datawriter ADD MEMBER [<identity-name>];ALTER ROLE db_ddladmin ADD MEMBER [<identity-name>];GO

<identity-name>is the name of the managed identity in Azure AD. If the identity is system-assigned, the name always the same as the name of your App Service app. To grant permissions for an Azure AD group, use the group's display name instead (for example,myAzureSQLDBAccessGroup).

TypeEXITto return to the Cloud Shell prompt.
The back-end services of managed identities alsomaintains a token cachethat updates the token for a target resource only when it expires. If you make a mistake configuring your SQL Database permissions and try to modify the permissionsaftertrying to get a token with your app, you don't actually get a new token with the updated permissions until the cached token expires.

Modify connection string

Remember that the same changes you made inWeb.configorappsettings.jsonworks with the managed identity, so the only thing to do is to remove the existing connection string in App Service, which Visual Studio created deploying your app the first time. Use the following command, but replace<app-name>with the name of your app.

az webapp config connection-string delete --resource-group myResourceGroup --name <app-name> --setting-names MyDbConnection

You could refer to our official documentation regarding this at Tutorial: Access data with managed identity - Azure App Service | Microsoft Docs

GitHub Link :

https://github.com/vijaysaayi/MSI-Validator/tree/master

msi-validator v1.0.0.0.zip

Managed Identity for Azure App Services (2024)

FAQs

How do I give managed identity access to app services in Azure? ›

Add a managed identity
  • Access your App Services resource in the Azure portal. ...
  • Scroll down to the Settings group in the left pane, and select Identity.
  • On the System assigned tab, switch Status to On and select Save.
  • When prompted, answer Yes to turn on the system-assigned managed identity.
Feb 20, 2024

Which Azure services support managed identities? ›

Services supporting managed identities
Service NameDocumentation
Azure Kubernetes Service (AKS)Use managed identities in Azure Kubernetes Service
Azure Load TestingUse managed identities for Azure Load Testing
Azure Logic AppsAuthenticate access to Azure resources using managed identities in Azure Logic Apps
53 more rows
Mar 14, 2024

What is identity in Azure App Service? ›

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.

How do I add managed identity to Azure function app? ›

Step 3: Add the user-assigned managed identity to Azure Functions
  1. In the Azure portal, go to your Azure function.
  2. Under Account Settings, select Identity.
  3. Select the User assigned tab, and then select Add.
  4. Select your existing user-assigned managed identity, and then select Add.
Oct 10, 2023

How do I restrict access to Azure App Service? ›

To add an access restriction rule to your app, do the following steps:
  1. Sign in to the Azure portal.
  2. Select the app that you want to add access restrictions to.
  3. On the left menu, select Networking.
  4. On the Networking page, under Inbound traffic configuration, select the Public network access setting.
Jan 29, 2024

How do I enable SSO in Azure App Service? ›

One-click SSO configuration steps
  1. Add the application from the Azure Marketplace.
  2. Select Single sign-on.
  3. Select Enable single sign-on.
  4. Populate the mandatory configuration values in the Basic SAML Configuration section.
Feb 26, 2024

How do I add an Identity provider to my Azure app Service? ›

Sign in to the Azure portal and navigate to your app. On your app's left menu, select Authentication, and then select Add identity provider. In the Add an identity provider page, select Microsoft as the Identity provider to sign in Microsoft and Microsoft Entra identities.

How to enable managed service identity? ›

To enable system-assigned managed identity on a VM during its creation, your account needs the Virtual Machine Contributor role assignment. No other Microsoft Entra directory role assignments are required. Under the Management tab in the Identity section, switch Managed service identity to On.

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.

How do I check managed identity permissions in Azure? ›

System-assigned managed identity

In the left menu, click Identity. Under Permissions, click Azure role assignments. If roles are already assigned to the selected system-assigned managed identity, you see the list of role assignments. This list includes all role assignments you have permission to read.

How to create managed identity in Azure for application Gateway? ›

The process involves the following four steps:
  1. Step 1: Create a Managed Identity.
  2. Step 2: Create Key Vault and Cert.
  3. Step 3: Create Access Policy and assign to Managed Identity.
  4. Step 4: Create Application Gateway.
  5. Step 1: Create a Managed Identity.
  6. Step 2: Create Key Vault. ...
  7. Step 3: Create Access Policy and assign to MI.
Mar 29, 2023

How do I grant access to an app in Azure? ›

Grant Read and Wirte permissions for Azure Web App
  1. Click Access control (IAM).
  2. Click the Role assignments tab to view the role assignments at this scope.
  3. Click Add > Add role assignment. ...
  4. Select the appropriate role.
  5. Select who needs access.
  6. Then Assign role and click save.
Jun 1, 2021

How do I add an Identity provider to my Azure App Service? ›

Sign in to the Azure portal and navigate to your app. On your app's left menu, select Authentication, and then select Add identity provider. In the Add an identity provider page, select Microsoft as the Identity provider to sign in Microsoft and Microsoft Entra identities.

How do I enable managed service identity in Azure? ›

Enable system-assigned managed identity on an existing VM
  1. Sign in to the Azure portal using an account associated with the Azure subscription that contains the VM.
  2. Navigate to the desired Virtual Machine and select Identity.
  3. Under System assigned, Status, select On and then click Save:
May 29, 2024

How do I allow access to Azure services? ›

Use PowerShell or the Azure CLI to create a firewall rule with start and end IP addresses set to 0.0. 0.0 if you're not using the portal. This option configures the firewall to allow all connections from Azure, including connections from the subscriptions of other customers.

Top Articles
Latest Posts
Article information

Author: Merrill Bechtelar CPA

Last Updated:

Views: 6439

Rating: 5 / 5 (70 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Merrill Bechtelar CPA

Birthday: 1996-05-19

Address: Apt. 114 873 White Lodge, Libbyfurt, CA 93006

Phone: +5983010455207

Job: Legacy Representative

Hobby: Blacksmithing, Urban exploration, Sudoku, Slacklining, Creative writing, Community, Letterboxing

Introduction: My name is Merrill Bechtelar CPA, I am a clean, agreeable, glorious, magnificent, witty, enchanting, comfortable person who loves writing and wants to share my knowledge and understanding with you.