Lesson 6 of 9
Environment Configuration - appsettings & Secrets
ASP.NET Core's configuration hierarchy lets you specify defaults in appsettings.json and override per environment. Never commit secrets to git - use User Secrets in development and Azure Key Vault in production. Environment variables in containers override file-based config.
Configuration Hierarchy & Secret Management
// ━━ Configuration hierarchy (later overrides earlier) ━━
// 1. appsettings.json → base defaults (committed to git)
// 2. appsettings.{Environment}.json → env-specific (committed, no secrets)
// 3. secrets.json → dev secrets (never committed)
// 4. Environment variables → container config (K8s ConfigMap/Secret)
// 5. Azure Key Vault → production secrets
// appsettings.json:
// { "Jwt": { "Issuer": "https://myapp.com", "AccessTokenMinutes": 15 } }
// appsettings.Production.json:
// { "Jwt": { "AccessTokenMinutes": 5 } } ← shorter token in prod
// ━━ User Secrets (development) ━━
// dotnet user-secrets init
// dotnet user-secrets set "Jwt:SecretKey" "dev-only-secret-32-chars-minimum"
// dotnet user-secrets set "ConnectionStrings:DefaultConnection" "Server=localhost;..."
// Stored in: %APPDATA%/Microsoft/UserSecrets/{UserSecretsId}/secrets.json
// NEVER in source control
// ━━ Azure Key Vault (production) ━━
builder.Configuration.AddAzureKeyVault(
new Uri($"https://{builder.Configuration["KeyVaultName"]}.vault.azure.net"),
new DefaultAzureCredential()); // uses managed identity in AKS - no credentials needed!
// Key Vault naming: Jwt--SecretKey → Jwt:SecretKey (double-dash = colon)
// Store: "Jwt--SecretKey", "ConnectionStrings--DefaultConnection", "Stripe--SecretKey"
// ━━ Strongly-typed options with startup validation ━━
builder.Services
.AddOptions<JwtOptions>()
.BindConfiguration("Jwt")
.ValidateDataAnnotations()
.ValidateOnStart(); // throw if invalid AT STARTUP, not on first use
public class JwtOptions
{
[Required, MinLength(32)]
public string SecretKey { get; init; } = "";
[Required]
public string Issuer { get; init; } = "";
[Required]
public string Audience { get; init; } = "";
[Range(5, 60)]
public int AccessTokenMinutes { get; init; } = 15;
[Range(1, 30)]
public int RefreshTokenDays { get; init; } = 7;
}
// ━━ Environment variable override in Docker ━━
// In docker-compose.yml:
// environment:
// Jwt__SecretKey: "${JWT_SECRET}" # from host .env file
// Jwt__Issuer: "https://myapp.com"
// Note: __ (double underscore) = : (colon) for nested config in env vars
// ━━ Feature flags with IConfiguration ━━
// appsettings.json: "Features": { "NewCheckout": false, "BetaSearch": true }
var featureEnabled = builder.Configuration.GetValue<bool>("Features:NewCheckout");
// Or use Microsoft.FeatureManagement for advanced flag targetingTip
Tip
Practice Environment Configuration appsettings Secrets in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
.NET provides a unified platform for building various application types
Practice Task
Note
Practice Task - (1) Write a working example of Environment Configuration appsettings Secrets from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Environment Configuration appsettings Secrets is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready dotnet code.
Finished this lesson?