Skip to content

Commit 5504990

Browse files
committed
Update Azure Key Vault integration and configuration settings; modify appsettings for consistency and add aspire.config.json
1 parent be85681 commit 5504990

File tree

5 files changed

+52
-30
lines changed

5 files changed

+52
-30
lines changed

11-azure-keyvault/Program.cs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,43 @@
1-
using Microsoft.Extensions.Configuration;
2-
using Microsoft.AspNetCore.Builder;
1+
using Azure.Extensions.AspNetCore.Configuration.Secrets;
32
using Azure.Identity;
4-
using Azure.Security.KeyVault;
5-
using Azure.Security.KeyVault.Secrets;
63

74
var builder = WebApplication.CreateBuilder(args);
85

96
// Add Application Insights telemetry
107
builder.Services.AddApplicationInsightsTelemetry();
118

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))
1412
{
15-
throw new InvalidOperationException("Configuration value 'AzureKeyVault:Name' is required.");
16-
}
13+
var keyVaultUri = new Uri($"https://{keyVaultName}.vault.azure.net/");
1714

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+
}
1931

20-
//Connect to your KeyVault using the URI
21-
builder.Configuration
22-
.AddAzureKeyVault(keyVaultUri, new DefaultAzureCredential());
2332
var app = builder.Build();
24-
var configuration = app.Configuration;
2533

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}");
2740

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}");

12-configuration-precedence/Program.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@
1313
DumpKey("Precedence:CommandLineOnly");
1414

1515
PrintHeader("Providers (Top = Highest Precedence)");
16-
int i = 0;
17-
foreach (var src in config.Sources.Reverse())
18-
Console.WriteLine($" {++i}. {src.GetType().Name}");
19-
20-
PrintHeader("Configuration Debug View");
21-
Console.WriteLine(config.GetDebugView(ctx =>
22-
ctx.Key.Contains("Secret", StringComparison.OrdinalIgnoreCase)
23-
? "****"
24-
: ctx.Value));
16+
var index = 0;
17+
foreach (var source in config.Sources.Reverse())
18+
Console.WriteLine($" {++index}. {source.GetType().Name}");
19+
20+
PrintHeader("Configuration Debug View (Precedence only)");
21+
var debugView = config.GetDebugView(ctx =>
22+
ctx.Key.Contains("Secret", StringComparison.OrdinalIgnoreCase) ? "****" : ctx.Value);
23+
24+
// Filter to only show Precedence keys
25+
foreach (var line in debugView.Split(Environment.NewLine))
26+
{
27+
if (line.Contains("Precedence", StringComparison.OrdinalIgnoreCase))
28+
Console.WriteLine(line);
29+
}
2530

2631
return;
2732

24-configuration-reload/appsettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"MySettings": {
3-
"Greeting": "Hello NDC London!",
4-
"DelaySeconds": 2,
3+
"Greeting": "Hello CodeStock!",
4+
"DelaySeconds": 4,
55
"Feature": {
66
"Enabled": false,
77
"Threshold": 5

28-aspire/AspireSample.AppHost/AspireSample.AppHost.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Aspire.AppHost.Sdk/13.1.0">
1+
<Project Sdk="Aspire.AppHost.Sdk/13.2.2">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>

28-aspire/aspire.config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"appHost": {
3+
"path": "AspireSample.AppHost/AspireSample.AppHost.csproj"
4+
}
5+
}

0 commit comments

Comments
 (0)