|
1 | | -using Microsoft.Extensions.Configuration; |
2 | | -using Microsoft.AspNetCore.Builder; |
| 1 | +using Azure.Extensions.AspNetCore.Configuration.Secrets; |
3 | 2 | using Azure.Identity; |
4 | | -using Azure.Security.KeyVault; |
5 | | -using Azure.Security.KeyVault.Secrets; |
6 | 3 |
|
7 | 4 | var builder = WebApplication.CreateBuilder(args); |
8 | 5 |
|
9 | 6 | // Add Application Insights telemetry |
10 | 7 | builder.Services.AddApplicationInsightsTelemetry(); |
11 | 8 |
|
12 | | -var keyVaultName = builder.Configuration.GetValue<string>("AzureKeyVault:Name"); |
13 | | -if (string.IsNullOrWhiteSpace(keyVaultName)) |
| 9 | +// Get Key Vault name from configuration (set via environment variable or appsettings) |
| 10 | +var keyVaultName = builder.Configuration["AzureKeyVault:Name"]; |
| 11 | +if (!string.IsNullOrWhiteSpace(keyVaultName)) |
14 | 12 | { |
15 | | - throw new InvalidOperationException("Configuration value 'AzureKeyVault:Name' is required."); |
16 | | -} |
| 13 | + var keyVaultUri = new Uri($"https://{keyVaultName}.vault.azure.net/"); |
17 | 14 |
|
18 | | -var keyVaultUri = new Uri($"https://{keyVaultName}.vault.azure.net/"); |
| 15 | + // Connect to Key Vault using DefaultAzureCredential |
| 16 | + // This supports managed identity in Azure and local development credentials |
| 17 | + builder.Configuration.AddAzureKeyVault( |
| 18 | + keyVaultUri, |
| 19 | + new DefaultAzureCredential(), |
| 20 | + new AzureKeyVaultConfigurationOptions |
| 21 | + { |
| 22 | + // Reload secrets every 5 minutes (optional - remove if not needed) |
| 23 | + ReloadInterval = TimeSpan.FromMinutes(5) |
| 24 | + }); |
| 25 | +} |
| 26 | +else if (!builder.Environment.IsDevelopment()) |
| 27 | +{ |
| 28 | + // Only require Key Vault in non-development environments |
| 29 | + throw new InvalidOperationException("Configuration value 'AzureKeyVault:Name' is required in non-development environments."); |
| 30 | +} |
19 | 31 |
|
20 | | -//Connect to your KeyVault using the URI |
21 | | -builder.Configuration |
22 | | - .AddAzureKeyVault(keyVaultUri, new DefaultAzureCredential()); |
23 | 32 | var app = builder.Build(); |
24 | | -var configuration = app.Configuration; |
25 | 33 |
|
26 | | -Console.WriteLine($"Message: {configuration["Message"]}"); |
| 34 | +// Access configuration values - Key Vault secrets override local config |
| 35 | +var message = app.Configuration["Message"] ?? "No message configured"; |
| 36 | +var greeting = app.Configuration["greeting"] ?? "Hello"; |
| 37 | +var environment = app.Configuration["environment"] ?? "unknown"; |
| 38 | + |
| 39 | +Console.WriteLine($"Message: {message}"); |
27 | 40 |
|
28 | | -var name = args.Any() ? args[0] : "World"; |
29 | | -// See https://aka.ms/new-console-template for more information |
30 | | -Console.WriteLine($"{configuration["greeting"]}, {name}."); |
31 | | -Console.WriteLine($"Environment: {configuration["environment"]}"); |
| 41 | +var name = args.Length > 0 ? args[0] : "World"; |
| 42 | +Console.WriteLine($"{greeting}, {name}."); |
| 43 | +Console.WriteLine($"Environment: {environment}"); |
0 commit comments