Ir al contenido

Get started with the Azure Key Vault integration

Esta página aún no está disponible en tu idioma.

Azure Key Vault logo

Azure Key Vault helps safeguard cryptographic keys and secrets used by cloud applications and services. The Aspire Azure Key Vault integration enables you to connect to existing Azure Key Vault instances.

In this introduction, you’ll see how to install and use the Aspire Azure Key Vault integrations in a simple configuration. If you already have this knowledge, see Azure Key Vault Hosting integration for full reference details.

To begin, install the Aspire Azure Key Vault Hosting integration in your Aspire AppHost project. This integration allows you to create and manage Azure Key Vault resources from your Aspire hosting projects:

Aspire CLI — Añadir paquete Aspire.Hosting.Azure.KeyVault
aspire add azure-keyvault

La CLI de Aspire es interactiva; asegúrate de seleccionar el resultado adecuado cuando se te pida:

Aspire CLI — Ejemplo de salida
Select an integration to add:
> azure-keyvault (Aspire.Hosting.Azure.KeyVault)
> Other results listed as selectable options...

Next, in the AppHost project, create an Azure Key Vault resource and pass it to the consuming client projects:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var keyVault = builder.AddAzureKeyVault("key-vault");
builder.AddProject<Projects.ExampleProject>()
.WithReference(keyVault);
// After adding all resources, run the app...
builder.Build().Run();

The preceding code adds an Azure Key Vault resource named key-vault to the AppHost project. The WithReference method passes the connection information to the ExampleProject project.

To use Azure Key Vault from your client applications, install the Aspire Azure Key Vault client integration in your client project:

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

The client integration provides two ways to access secrets from Azure Key Vault:

  • Add secrets to app configuration, using either the IConfiguration or the IOptions<T> pattern.
  • Use a SecretClient to retrieve secrets on demand.

In the Program.cs file of your client-consuming project, call the AddAzureKeyVaultSecrets extension method on the IConfiguration to add the secrets as part of your app’s configuration:

builder.Configuration.AddAzureKeyVaultSecrets(connectionName: "key-vault");

You can then retrieve a secret-based configuration value through the normal IConfiguration APIs, or by binding to strongly-typed classes with the options pattern.

Alternatively, you can use the SecretClient directly to retrieve the secrets on demand:

builder.AddAzureKeyVaultClient(connectionName: "key-vault");

In the AppHost, when you used the WithReference method to pass an Azure Key Vault resource to a consuming client project, Aspire injects several configuration properties that you can use in the consuming project.

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

Use the GetValue() method to obtain these environment variables in consuming projects:

C# — Obtain configuration properties
string vaultUri = builder.Configuration.GetValue<string>("KEYVAULT_URI");

Use Azure Key Vault resources in client code

Section titled “Use Azure Key Vault resources in client code”

After adding the SecretClient to the builder, you can get the SecretClient instance using dependency injection:

public class ExampleService(SecretClient client)
{
// Use client...
}

For full details on using the client integration, see Azure Key Vault Client integration.