コンテンツにスキップ
Docs Try Aspire
Docs Try

Connect to Azure Key Vault

このコンテンツはまだ日本語訳がありません。

Azure Key Vault logo

This page describes how consuming apps connect to an Azure Key Vault resource that’s already modeled in your AppHost. For the AppHost API surface — adding a Key Vault resource, role assignments, secret references, and infrastructure customization — see Azure Key Vault Hosting integration.

When you reference an Azure Key Vault resource from your AppHost, Aspire injects the vault URI into the consuming app as an environment variable. Your app can either read that environment variable directly — the pattern works the same from any language — or, in C#, use the Aspire Azure Key Vault client integration for automatic dependency injection, health checks, and telemetry.

Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Uri property of a resource called key-vault becomes KEY_VAULT_URI.

The Azure Key Vault resource exposes the following connection property:

Property NameDescription
UriThe Key Vault endpoint URI, typically https://<vault-name>.vault.azure.net/

Example connection value:

Uri: https://keyvault-abc123.vault.azure.net/

Pick the language your consuming app is written in. Each example assumes your AppHost adds an Azure Key Vault resource named key-vault and references it from the consuming app.

For C# apps, the recommended approach is the Aspire Azure Key Vault client integration. It registers an SecretClient through dependency injection and adds health checks and telemetry automatically. The integration also provides an optional configuration provider that surfaces vault secrets through the standard IConfiguration API.

Install the 📦 Aspire.Azure.Security.KeyVault NuGet package in the client-consuming project:

.NET CLI — Add Aspire.Azure.Security.KeyVault package
dotnet add package Aspire.Azure.Security.KeyVault

In Program.cs, call AddAzureKeyVaultClient on your IHostApplicationBuilder to register a SecretClient:

C# — Program.cs
builder.AddAzureKeyVaultClient(connectionName: "key-vault");

Resolve the SecretClient through dependency injection:

C# — ExampleService.cs
public class ExampleService(SecretClient secretClient)
{
public async Task<string> GetSecretAsync(string secretName)
{
var secret = await secretClient.GetSecretAsync(secretName);
return secret.Value.Value;
}
}

Alternatively, call AddAzureKeyVaultSecrets to surface vault secrets through the IConfiguration API. This is useful for reading secrets with the options pattern or IConfiguration directly:

C# — Program.cs
builder.Configuration.AddAzureKeyVaultSecrets(connectionName: "key-vault");

You can then read secrets by name through IConfiguration or bind them to strongly-typed options:

C# — Using IConfiguration
public class ExampleService(IConfiguration configuration)
{
private string _apiKey = configuration["my-secret-name"];
}
C# — Using IOptions<T>
public class ExampleService(IOptions<MyOptions> options)
{
private string _apiKey = options.Value.MySecretName;
}

To register multiple SecretClient instances with different vault names, use AddKeyedAzureKeyVaultClient:

C# — Program.cs
builder.AddKeyedAzureKeyVaultClient(name: "feature-toggles");
builder.AddKeyedAzureKeyVaultClient(name: "admin-portal");

Then resolve each instance by key:

C# — ExampleService.cs
public class ExampleService(
[FromKeyedServices("feature-toggles")] SecretClient featureTogglesClient,
[FromKeyedServices("admin-portal")] SecretClient adminPortalClient)
{
// Use clients...
}

For more information, see .NET dependency injection: Keyed services.

The Aspire Azure Key Vault client integration offers multiple ways to provide configuration.

Connection strings. When using a connection string from the ConnectionStrings configuration section, pass the connection name to AddAzureKeyVaultClient:

C# — Program.cs
builder.AddAzureKeyVaultClient("key-vault");

The connection string is resolved from the ConnectionStrings section:

JSON — appsettings.json
{
"ConnectionStrings": {
"key-vault": "https://myvault.vault.azure.net/"
}
}

Configuration providers. The client integration supports Microsoft.Extensions.Configuration. It loads AzureSecurityKeyVaultSettings from appsettings.json (or any other configuration source) by using the Aspire:Azure:Security:KeyVault key:

JSON — appsettings.json
{
"Aspire": {
"Azure": {
"Security": {
"KeyVault": {
"DisableHealthChecks": true,
"DisableTracing": false,
"ClientOptions": {
"Diagnostics": {
"ApplicationId": "myapp"
}
}
}
}
}
}
}

Inline delegates. Pass an Action<AzureSecurityKeyVaultSettings> to configure settings inline:

C# — Program.cs
builder.AddAzureKeyVaultClient(
connectionName: "key-vault",
configureSettings: settings => settings.DisableHealthChecks = true);

Aspire client integrations enable health checks by default. The Azure Key Vault client integration adds:

  • The AzureKeyVaultSecretsHealthCheck, which attempts to connect to and query the vault.
  • Integration with the /health HTTP endpoint, where all registered health checks must pass before the app is considered ready to accept traffic.

The Aspire Azure Key Vault client integration automatically configures logging and tracing through OpenTelemetry.

Logging categories:

  • Azure.Core
  • Azure.Identity

Tracing activities:

  • Azure.Security.KeyVault.Secrets.SecretClient

Metrics: The Azure Key Vault integration currently doesn’t support metrics by default due to limitations with the Azure SDK.