Configuration Hierarchy
ASP.NET Core loads configuration in the following order (later sources override earlier ones):appsettings.json
Base configuration - Shared across all environments, committed to source control.Contains non-sensitive defaults like:
- Logging levels
- OpenTelemetry configuration structure
- Module settings
- Feature flags
appsettings.{Environment}.json
Environment-specific overrides - Loaded based on
ASPNETCORE_ENVIRONMENT.appsettings.Development.json- Dev overrides (CORS, OpenAPI enabled)appsettings.Production.json- Prod overrides (security hardening)
Environment Variables
Runtime configuration - Highest priority, overrides JSON files.Use for:
- Secrets (connection strings, API keys, JWT keys)
- Environment-specific URLs
- Cloud provider configurations
__) notation:Configuration Priority: Secrets Manager (via env vars) > Environment Variables >
appsettings.{Environment}.json > appsettings.jsonConfiguration Files
appsettings.json (Base)
Contains shared configuration and non-sensitive defaults:Environment Variables
Naming Convention
ASP.NET Core uses double-underscore (__) to represent nested configuration:
Common Configuration Variables
- Database
- Redis Cache
- JWT Authentication
- CORS & Origins
- Storage
- OpenTelemetry
Setting Environment Variables
Secrets Management
AWS Secrets Manager
The Terraform configuration automatically manages secrets:RDS Password (AWS-Managed)
When The full connection string is injected into ECS tasks via the
db_manage_master_user_password = true, AWS generates and stores the password:terraform/apps/playground/app_stack/main.tf
secrets configuration.Azure Key Vault
For Azure deployments:Configuration Best Practices
1. Never Commit Secrets
1. Never Commit Secrets
DO:
- Use
appsettings.Production.jsonwith empty strings for secrets - Store secrets in AWS Secrets Manager, Azure Key Vault, or environment variables
- Add
.envfiles to.gitignore
- Commit passwords, API keys, or connection strings to source control
- Use placeholder values like
passwordorsecretin production config - Store secrets in Docker images
2. Use Separate Configs per Environment
2. Use Separate Configs per Environment
Create environment-specific configurations:Set
ASPNETCORE_ENVIRONMENT to load the correct file:3. Validate Configuration on Startup
3. Validate Configuration on Startup
Add validation to catch configuration errors early:
4. Use Connection String Builders
4. Use Connection String Builders
Build connection strings programmatically for better control:
5. Document Required Configuration
5. Document Required Configuration
Maintain a configuration reference:
Production Configuration Checklist
Before deploying to production:Security
✅
ASPNETCORE_ENVIRONMENT=Production set✅ All secrets stored in Secrets Manager or Key Vault✅ JWT signing key is strong (min 32 characters) and never committed✅ Database connection uses SSL/TLS (SSL Mode=Require)✅ Redis connection uses SSL if available✅ CORS configured with explicit allowed origins (no AllowAll)✅ AllowedHosts set to your domainFeatures
✅ OpenAPI/Swagger disabled (
OpenApiOptions__Enabled=false)✅ Rate limiting enabled (RateLimitingOptions__Enabled=true)✅ Tenant migrations disabled on startup (MultitenancyOptions__RunTenantMigrationsOnStartup=false)✅ Storage provider set to cloud (s3 or azure, not local)Observability
✅ OpenTelemetry configured with production collector endpoint✅ Serilog minimum level set to
Information (not Debug)✅ CloudWatch Logs or equivalent configured✅ Health checks configured (/health/live, /health/ready)Performance
✅ Redis caching configured and tested✅ Database connection pooling enabled✅ S3/Azure storage with CDN (CloudFront/Azure CDN)✅ ECS auto-scaling policies configured
Troubleshooting
Configuration value not loading
Configuration value not loading
Problem: Environment variable or secret not being read.Debug:Common issues:
- Typo in environment variable name (check double underscores)
- Wrong
ASPNETCORE_ENVIRONMENTvalue - Secrets Manager permission denied (check IAM role)
Secrets Manager access denied
Secrets Manager access denied
Error:
User: arn:aws:sts::123456789012:assumed-role/... is not authorized to perform: secretsmanager:GetSecretValueSolution: Grant ECS task execution role access to secrets:Connection string format errors
Connection string format errors
Error:
Keyword not supported: 'server'Cause: Using SQL Server connection string format with PostgreSQL (or vice versa).Solution:- PostgreSQL:
Host=...;Port=5432;Database=...;Username=...;Password=... - SQL Server:
Server=...;Database=...;User Id=...;Password=...
Array configuration not working
Array configuration not working
Problem: Or use JSON in environment variable:
CorsOptions__AllowedOrigins__0 not loading.Solution: Use index notation for arrays:Next Steps
AWS Deployment
Learn how Terraform automatically configures secrets and environment variables
Docker Deployment
Set up environment variables and secrets for Docker Compose deployments
Deployment Overview
Review production considerations and deployment best practices
Security Best Practices
Deep dive into authentication, authorization, and security hardening
