Watch Aspire live streams문서사용해 보기

User-assigned managed identity

이 콘텐츠는 아직 번역되지 않았습니다.

Azure Managed Identity logo

This article is the reference for the Aspire Azure user-assigned managed identity (UMI) support. It enumerates the AppHost APIs — with examples for both AppHost.cs and apphost.mts — that you use to add, reference, and assign roles to user-assigned managed identities in your AppHost project.

A user-assigned managed identity is a standalone Azure resource that you assign to one or more Azure service resources, giving you explicit control over identity management and resource access.

To create a new user-assigned managed identity, use the AddAzureUserAssignedIdentity (or addAzureUserAssignedIdentity) API:

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const sharedMi = await builder.addAzureUserAssignedIdentity("custom-umi");
// After adding all resources, run the app...
await builder.build().run();

The preceding code creates a new managed identity named "custom-umi" that you can use with other resources in your application.

If you already have a managed identity, reference it using the PublishAsExisting (or publishAsExisting) method. This is useful when you want to use an identity created outside of your Aspire project:

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const miName = await builder.addParameter("miName");
const miResourceGroup = await builder.addParameter("miResourceGroup");
const sharedMi = await builder.addAzureUserAssignedIdentity("custom-umi");
await sharedMi.publishAsExisting(miName, miResourceGroup);
// After adding all resources, run the app...
await builder.build().run();

In the preceding example, parameters supply the name and resource group of the existing identity so the AppHost references it rather than creating a new one.

Grant Azure roles to your managed identity using the WithRoleAssignments (or withRoleAssignments) API, giving the identity access to other Azure resources:

apphost.mts
import { createBuilder, AzureKeyVaultRole } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const sharedMi = await builder.addAzureUserAssignedIdentity("custom-umi");
const secrets = await builder.addAzureKeyVault("secrets");
await sharedMi.withRoleAssignments(secrets, [AzureKeyVaultRole.KeyVaultSecretsUser]);
// After adding all resources, run the app...
await builder.build().run();

In the preceding example, the managed identity is granted the KeyVaultSecretsUser role on the Key Vault resource.