|
| 1 | +--- |
| 2 | +title: Authenticate Azure-hosted .NET apps to Azure resources using a user-assigned managed identity |
| 3 | +description: Learn how to authenticate Azure-hosted .NET apps to other Azure services using a user-assigned managed identity. |
| 4 | +ms.topic: how-to |
| 5 | +ms.custom: devx-track-dotnet, engagement-fy23, devx-track-azurecli |
| 6 | +ms.date: 02/06/2025 |
| 7 | +--- |
| 8 | + |
| 9 | +# Authenticate Azure-hosted .NET apps to Azure resources using a user-assigned managed identity |
| 10 | + |
| 11 | +The recommended approach to authenticate an Azure-hosted app to other Azure resources is to use a [managed identity](/entra/identity/managed-identities-azure-resources/overview). This approach is [supported for most Azure services](/entra/identity/managed-identities-azure-resources/managed-identities-status), including apps hosted on Azure App Service, Azure Container Apps, and Azure Virtual Machines. Discover more about different authentication techniques and approaches on the [authentication overview](/dotnet/azure/sdk/authentication) page. In the sections ahead, you'll learn: |
| 12 | + |
| 13 | +- Essential managed identity concepts |
| 14 | +- How to create a user-assigned managed identity for your app |
| 15 | +- How to assign roles to the user-assigned managed identity |
| 16 | +- How to authenticate using the user-assigned managed identity from your app code |
| 17 | + |
| 18 | +[!INCLUDE [managed-identity-concepts](../includes/managed-identity-concepts.md)] |
| 19 | + |
| 20 | +The sections ahead describe the steps to enable and use a user-assigned managed identity for an Azure-hosted app. If you need to use a user-assigned managed identity, visit the [system-assigned managed identities](/dotnet/azure/sdk/authentication/system-assigned-managed-identity) article for more information. |
| 21 | + |
| 22 | +## Create a user-assigned managed identity |
| 23 | + |
| 24 | +User-assigned managed identities are created as standalone resources in your Azure subscription using the Azure portal or the Azure CLI. Azure CLI commands can be run in the [Azure Cloud Shell](https://shell.azure.com) or on a workstation with the [Azure CLI installed](/cli/azure/install-azure-cli). |
| 25 | + |
| 26 | +### [Azure portal](#tab/azure-portal) |
| 27 | + |
| 28 | +1. In the Azure portal, enter *Managed identities* in the main search bar and select the matching result under the **Services** section. |
| 29 | +1. On the **Managed Identities** page, select **+ Create**. |
| 30 | + |
| 31 | + :::image type="content" source="../media/user-assigned-identity-create.png" alt-text="A screenshot showing the page to manage user-assigned identities."::: |
| 32 | + |
| 33 | +1. On the **Create User Assigned Managed Identity** page, select a subscription, resource group, and region for the user-assigned managed identity, and then provide a name. |
| 34 | +1. Select **Review + create** to review and validate your inputs. |
| 35 | + |
| 36 | + :::image type="content" source="../media/user-assigned-identity-form.png" alt-text="A screenshot showing the form to create a user-assigned managed identity."::: |
| 37 | + |
| 38 | +1. Select **Create** to create the user-assigned managed identity. |
| 39 | +1. After the identity is created, select **Go to resource**. |
| 40 | +1. On the new identity's **Overview** page, copy the **Client ID** value to use for later when you configure the application code. |
| 41 | + |
| 42 | +### [Azure CLI](#tab/azure-cli) |
| 43 | + |
| 44 | +Use the Azure CLI command [`az identity create`](/cli/azure/identity?view=azure-cli-latest#az-identity-create) to create a managed identity: |
| 45 | + |
| 46 | +```azurecli |
| 47 | +az identity create \ |
| 48 | + --resource-group <resource-group-name> \ |
| 49 | + --name <identity-name> \ |
| 50 | + --query 'clientId' \ |
| 51 | + --output json |
| 52 | +``` |
| 53 | + |
| 54 | +The command output prints the client ID of the created user-assigned managed identity. The client ID is used to configure application code that relies on the identity. |
| 55 | + |
| 56 | +You can always view the managed identity properties again using the [`az identity show`](/cli/azure/identity?view=azure-cli-latest#az-identity-show) command: |
| 57 | + |
| 58 | +```azurecli |
| 59 | +az identity show \ |
| 60 | + --resource-group <your-resource-group> \ |
| 61 | + --name <your-managed-identity-name> \ |
| 62 | + --output json |
| 63 | +``` |
| 64 | + |
| 65 | +--- |
| 66 | + |
| 67 | +## Assign the managed identity to your app |
| 68 | + |
| 69 | +A user-assigned managed identity can be associated with one or more Azure resources. All of the resources that use that identity gain the permissions applied through the identity's roles. |
| 70 | + |
| 71 | +### [Azure portal](#tab/azure-portal) |
| 72 | + |
| 73 | +1. In the Azure portal, navigate to the resource that hosts your app code, such as an Azure App Service or Azure Container App instance. |
| 74 | +1. From the resource's **Overview** page, expand **Settings** and select **Identity** from the navigation. |
| 75 | +1. On the **Identity** page, switch to the **User assigned** tab. |
| 76 | +1. Select **+ Add** to open the **Add user assigned managed identity** panel. |
| 77 | +1. On the **Add user assigned managed identity** panel, use the **Subscription** dropdown to filter the search results for your identities. Use the **User assigned managed identities** search box to locate the user-assigned managed identity you enabled for the Azure resource hosting your app. |
| 78 | +1. Select the identity and choose **Add** at the bottom of the panel to continue. |
| 79 | + |
| 80 | + :::image type="content" source="../media/add-user-assigned-identity-to-app.png" alt-text="A screenshot showing how to associate a user-assigned identity with an app."::: |
| 81 | + |
| 82 | +### [Azure CLI](#tab/azure-cli) |
| 83 | + |
| 84 | +The Azure CLI provides different commands to assign a user-assigned managed identity to different types of hosting services. |
| 85 | + |
| 86 | +To assign a user-assigned managed identity to a resource such as an Azure App Service web app using the Azure CLI, you'll need the resource ID of the identity. Use the [`az identity show`](/cli/azure/identity?view=azure-cli-latest#az-identity-show) command to retrieve the resource ID: |
| 87 | + |
| 88 | +```azurecli |
| 89 | +az identity show \ |
| 90 | + --resource-group <your-resource-group> \ |
| 91 | + --name <your-managed-identity-name> \ |
| 92 | + --output json \ |
| 93 | + --query id |
| 94 | +``` |
| 95 | + |
| 96 | +Once you have the resource ID, use the Azure CLI command `az <resourceType> identity assign` command to associate the user-assigned managed identity with different resources, such as the following: |
| 97 | + |
| 98 | +For Azure App Service, use the Azure CLI command [`az webapp identity assign`](/cli/azure/webapp/identity?view=azure-cli-latest#az-webapp-identity-assign): |
| 99 | + |
| 100 | +```azurecli |
| 101 | +az webapp identity assign \ |
| 102 | + --resource-group <resource-group-name> \ |
| 103 | + --name <webapp-name> \ |
| 104 | + --identities <user-assigned-identity-resource-id> |
| 105 | +``` |
| 106 | + |
| 107 | +For Azure Container Apps, use the Azure CLI command [`az containerapp identity assign`](/cli/azure/containerapp/identity?view=azure-cli-latest#az-containerapp-identity-assign): |
| 108 | + |
| 109 | +```azurecli |
| 110 | +az containerapp identity assign \ |
| 111 | + --resource-group <resource-group-name> \ |
| 112 | + --name <containerapp-name> \ |
| 113 | + --identities <user-assigned-identity-resource-id> |
| 114 | +``` |
| 115 | + |
| 116 | +For Azure Virtual Machines, use the Azure CLI command [`az vm identity assign`](/cli/azure/vm/identity?view=azure-cli-latest#az-vm-identity-assign): |
| 117 | + |
| 118 | +```azurecli |
| 119 | +az vm identity assign \ |
| 120 | + --resource-group <resource-group-name> \ |
| 121 | + --name <vm-name> \ |
| 122 | + --identities <user-assigned-identity-resource-id> |
| 123 | +``` |
| 124 | + |
| 125 | +--- |
| 126 | + |
| 127 | +## Assign roles to the managed identity |
| 128 | + |
| 129 | +Next, determine which roles your app needs and assign those roles to the managed identity. You can assign roles to a managed identity at the following scopes: |
| 130 | + |
| 131 | +- **Resource**: The assigned roles only apply to that specific resource. |
| 132 | +- **Resource group**: The assigned roles apply to all resources contained in the resource group. |
| 133 | +- **Subscription**: The assigned roles apply to all resources contained in the subscription. |
| 134 | + |
| 135 | +The following example shows how to assign roles at the resource group scope, since many apps manage all their related Azure resources using a single resource group. |
| 136 | + |
| 137 | +### [Azure portal](#tab/azure-portal) |
| 138 | + |
| 139 | +1. Navigate to the **Overview** page of the resource group that contains the app with the user-assigned managed identity. |
| 140 | +1. Select **Access control (IAM)** on the left navigation. |
| 141 | +1. On the **Access control (IAM)** page, select **+ Add** on the top menu and then choose **Add role assignment** to navigate to the **Add role assignment** page. |
| 142 | + |
| 143 | + :::image type="content" source="../media/add-role-assignment.png" alt-text="A screenshot showing how to access the identity role assignment page."::: |
| 144 | + |
| 145 | +1. The **Add role assignment** page presents a tabbed, multi-step workflow to assign roles to identities. On the initial **Role** tab, use the search box at the top to locate the role you want to assign to the identity. |
| 146 | +1. Select the role from the results and then choose **Next** to move to the **Members** tab. |
| 147 | +1. For the **Assign access to** option, select **Managed identity**. |
| 148 | +1. For the **Members** option, choose **+ Select members** to open the **Select managed identities** panel. |
| 149 | +1. On the **Select managed identities** panel, use the **Subscription** and **Managed identity** dropdowns to filter the search results for your identities. Use the **Select** search box to locate the user-assigned managed identity you enabled for the Azure resource hosting your app. |
| 150 | + |
| 151 | + :::image type="content" source="../media/user-assigned-identity-assign-roles.png" alt-text="A screenshot showing the managed identity assignment process."::: |
| 152 | + |
| 153 | +1. Select the identity and choose **Select** at the bottom of the panel to continue. |
| 154 | +1. Select **Review + assign** at the bottom of the page. |
| 155 | +1. On the final **Review + assign** tab, select **Review + assign** to complete the workflow. |
| 156 | + |
| 157 | +### [Azure CLI](#tab/azure-cli) |
| 158 | + |
| 159 | +To assign a role to a user-assigned managed identity using the Azure CLI, you'll need the principal ID of the identity. Use the `az identity show` command to retrieve the resource ID: |
| 160 | + |
| 161 | +```azurecli |
| 162 | +az identity show \ |
| 163 | + --resource-group <your-resource-group> \ |
| 164 | + --name <your-managed-identity-name> \ |
| 165 | + --output json \ |
| 166 | + --query principalId |
| 167 | +``` |
| 168 | + |
| 169 | +Assign a role to a managed identity using the [az role assignment create](/cli/azure/role/assignment#az-role-assignment-create) command: |
| 170 | + |
| 171 | +```azurecli |
| 172 | +az role assignment create \ |
| 173 | + --assignee <your-principal-id> \ |
| 174 | + --role <role-name> \ |
| 175 | + --scope <scope> |
| 176 | +``` |
| 177 | + |
| 178 | +To explore which roles a managed identity can be assigned, use the [az role definition list](/cli/azure/role/definition#az-role-definition-list) command: |
| 179 | + |
| 180 | +```azurecli |
| 181 | +az role definition list \ |
| 182 | + --query "sort_by([].{roleName:roleName, description:description}, &roleName)" \ |
| 183 | + --output table |
| 184 | +``` |
| 185 | + |
| 186 | +For example, to allow the managed identity with the ID of `99999999-9999-9999-9999-999999999999` read, write, and delete access to Azure Storage blob containers and data to all storage accounts in the *msdocs-dotnet-sdk-auth-example* resource group, assign the application service principal to the *Storage Blob Data Contributor* role using the following command: |
| 187 | + |
| 188 | +```azurecli |
| 189 | +az role assignment create \ |
| 190 | + --assignee 99999999-9999-9999-9999-999999999999 \ |
| 191 | + --role "Storage Blob Data Contributor" \ |
| 192 | + --scope "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/msdocs-dotnet-sdk-auth-example" |
| 193 | +``` |
| 194 | + |
| 195 | +For information on assigning permissions at the resource or subscription level using the Azure CLI, see the article [Assign Azure roles using the Azure CLI](/azure/role-based-access-control/role-assignments-cli). |
| 196 | + |
| 197 | +--- |
| 198 | + |
| 199 | +## Implement DefaultAzureCredential in your application |
| 200 | + |
| 201 | +[!INCLUDE [Implement DefaultAzureCredential](<../includes/implement-user-assigned-identity.md>)] |
0 commit comments