Enhanced .env file support with encryption, validation, and environment-specific configurations. Secure your application secrets while maintaining developer productivity.
Go beyond basic .env files with encryption, validation, and environment-specific configurations.
AES-256 encryption for sensitive values like API keys, database passwords, and OAuth secrets.
Support for .env.development, .env.production, and .env.testing files with automatic environment detection.
Ensure critical environment variables are present and valid before application startup.
Install Noundry.DotEnvX and start managing your application configuration securely.
using Noundry.DotEnvX.Core.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Load environment variables with encryption support
builder.Configuration.AddDotEnvX(options =>
{
options.Path = ".env";
options.EnvironmentSpecific = true;
options.Required = new[] {
"DATABASE_URL",
"JWT_SECRET"
};
options.EncryptionKey = Environment.GetEnvironmentVariable("DOTENVX_KEY");
});
// Now you can use configuration values
var connectionString = builder.Configuration.GetConnectionString("Database");
var jwtSecret = builder.Configuration["JWT_SECRET"];
builder.Services.AddSingleton(connectionString);
var app = builder.Build();
app.Run();
Manage different configurations for development, testing, staging, and production environments.
Local development configuration with debugging enabled and test data.
LocalDB or SQL Server Express
Development OAuth app credentials
Verbose logging and debug features
# Development Database
DATABASE_URL=Server=localhost;Database=MyApp_Dev;Integrated Security=true;TrustServerCertificate=true;
# Development OAuth (test credentials)
GOOGLE_CLIENT_ID=123456789-dev.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=dev-secret-not-encrypted
# JWT Secret for development
JWT_SECRET=development-jwt-secret-key-not-for-production
# Debug settings
ASPNETCORE_ENVIRONMENT=Development
LOGGING_LEVEL=Debug
ENABLE_SWAGGER=true
# Application URLs
BASE_URL=https://localhost:5001
API_URL=https://localhost:5001/api
Isolated testing configuration with test databases and mock services.
In-memory or dedicated test database
Mocked external API credentials
Optimized for test performance
# Testing Database (in-memory)
DATABASE_URL=Data Source=:memory:
USE_IN_MEMORY_DATABASE=true
# Mock OAuth credentials
GOOGLE_CLIENT_ID=test-client-id
GOOGLE_CLIENT_SECRET=test-secret
# Test JWT Secret
JWT_SECRET=test-jwt-secret-for-unit-tests
# Test Environment Settings
ASPNETCORE_ENVIRONMENT=Testing
LOGGING_LEVEL=Warning
ENABLE_SWAGGER=false
# Fast test execution
DISABLE_HTTPS_REDIRECT=true
SKIP_MIGRATIONS=true
Secure production configuration with encrypted secrets and monitoring.
All sensitive values encrypted with AES-256
Managed database with connection pooling
Error tracking and performance monitoring
# Production Database (encrypted)
DATABASE_URL="encrypted:AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAA..."
# Production OAuth (all encrypted)
GOOGLE_CLIENT_ID=123456789-prod.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET="encrypted:BDb7t3QkTRp2AbCdEfGhIjKlMnOpQrSt..."
MICROSOFT_CLIENT_ID=12345678-1234-1234-1234-123456789abc
MICROSOFT_CLIENT_SECRET="encrypted:XyZ9w8v7u6t5s4r3q2p1o0n9m8l7k6j..."
# Production JWT Secret (encrypted)
JWT_SECRET="encrypted:Gh1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p..."
# Production Settings
ASPNETCORE_ENVIRONMENT=Production
LOGGING_LEVEL=Information
ENABLE_SWAGGER=false
# Application URLs
BASE_URL=https://myapp.com
API_URL=https://api.myapp.com
# Monitoring
APPLICATION_INSIGHTS_KEY="encrypted:Mn2o3p4q5r6s7t8u9v0w1x2y3z4a5b6c..."
Protect sensitive configuration values with AES-256 encryption and secure key management.
Use the DotEnvX CLI tool to encrypt sensitive values before committing them to version control.
Secure key storage and distribution for different environments and team members.
Store encryption key in DOTENVX_KEY environment variable
Inject keys through build pipeline secrets
Integration with Azure Key Vault, AWS Secrets Manager
// Key management strategies
// 1. Environment variable (recommended)
DOTENVX_KEY=your-encryption-key-here
// 2. Configuration method
builder.Configuration.AddDotEnvX(options =>
{
options.EncryptionKey = GetEncryptionKeyFromVault();
});
// 3. Azure Key Vault integration
builder.Configuration.AddDotEnvX(options =>
{
options.EncryptionKey = builder.Configuration["Azure:KeyVault:DotEnvXKey"];
});
// 4. AWS Secrets Manager
options.EncryptionKey = await GetAwsSecret("dotenvx-key");
Real-world configuration examples for different application scenarios.
Start with a basic .env file and gradually add encryption and validation as needed.
// Program.cs - Basic setup
using Noundry.DotEnvX.Core.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Load .env file (simple)
builder.Configuration.AddDotEnvX();
// Or with options
builder.Configuration.AddDotEnvX(options =>
{
options.Path = ".env";
options.EnvironmentSpecific = true;
options.Required = new[] { "DATABASE_URL" };
});
// Access values like normal configuration
var dbUrl = builder.Configuration["DATABASE_URL"];
var apiKey = builder.Configuration["API_KEY"];
builder.Services.AddDbContext(dbUrl);
var app = builder.Build();
app.Run();
# Basic application configuration
DATABASE_URL=Server=localhost;Database=MyApp;Integrated Security=true;
API_KEY=sk_test_abcdef123456789
REDIS_URL=localhost:6379
# Application settings
APP_NAME=My Application
APP_VERSION=1.0.0
DEBUG_MODE=true
# Feature flags
ENABLE_FEATURE_X=true
MAX_UPLOAD_SIZE=10485760
// Access configuration values
var appName = builder.Configuration["APP_NAME"];
var debugMode = builder.Configuration.GetValue<bool>("DEBUG_MODE");
var maxUpload = builder.Configuration.GetValue<int>("MAX_UPLOAD_SIZE");
// Use in service configuration
builder.Services.AddDbContext(options =>
options.UseSqlServer(builder.Configuration["DATABASE_URL"]));
// Configure based on feature flags
if (builder.Configuration.GetValue<bool>("ENABLE_FEATURE_X"))
{
builder.Services.AddFeatureX();
}
Configuration for a web application using Noundry.UI, Noundry.Authnz, and Noundry.Tuxedo.
# Database Configuration
DATABASE_URL=Server=localhost;Database=EcommerceApp;Integrated Security=true;
REDIS_URL=localhost:6379
# OAuth Providers (encrypted in production)
GOOGLE_CLIENT_ID=123456789-dev.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET="encrypted:BDb7t3QkTRp2AbCdEfGhIjKl..."
MICROSOFT_CLIENT_ID=12345678-1234-1234-1234-123456789abc
MICROSOFT_CLIENT_SECRET="encrypted:XyZ9w8v7u6t5s4r3q2p1o0n..."
GITHUB_CLIENT_ID=Iv1.abcdef123456789
GITHUB_CLIENT_SECRET="encrypted:Gh1b2c3d4e5f6g7h8i9j0k1l..."
# JWT Configuration
JWT_SECRET="encrypted:Jw1t2s3c4r5t6k7y8f9r0a1p2p3l4c5t..."
JWT_EXPIRATION_DAYS=30
DEFAULT_REDIRECT_URI=/Dashboard
# Third-party Services
STRIPE_API_KEY="encrypted:Sk_1t2st3_abc123def456..."
SENDGRID_API_KEY="encrypted:SG.abc123def456..."
AZURE_STORAGE_CONNECTION="encrypted:DefaultEndpointsProtocol=https..."
# Application Settings
APP_NAME=E-Commerce Platform
COOKIE_DOMAIN=.myapp.com
ENABLE_ANALYTICS=true
Configuration for a high-performance API service with authentication, caching, and monitoring.
# Database & Caching
DATABASE_URL="encrypted:Server=prod-db.company.com;Database=ApiDb..."
REDIS_URL="encrypted:redis-prod.company.com:6380,password=..."
# API Authentication
JWT_SECRET="encrypted:SuperSecureJwtKeyForProductionApi..."
API_KEY_HEADER=X-API-Key
RATE_LIMIT_REQUESTS=1000
RATE_LIMIT_WINDOW=3600
# External Services
PAYMENT_API_KEY="encrypted:PaymentProviderSecretKey..."
EMAIL_API_KEY="encrypted:EmailServiceApiKey..."
STORAGE_CONNECTION="encrypted:BlobStorageConnectionString..."
# Monitoring & Logging
APPLICATION_INSIGHTS_KEY="encrypted:AppInsightsInstrumentationKey..."
LOG_LEVEL=Information
ENABLE_TELEMETRY=true
# Performance Settings
CONNECTION_POOL_SIZE=100
CACHE_DURATION_MINUTES=30
ENABLE_COMPRESSION=true
Separate configuration for unit tests and integration tests with mocked services.
// TestStartup.cs - Test-specific configuration
public class TestStartup
{
public void ConfigureServices(IServiceCollection services)
{
var configuration = new ConfigurationBuilder()
.AddDotEnvX(options =>
{
options.Path = ".env.testing";
options.Required = new[] { "TEST_DATABASE_URL" };
})
.Build();
// Use in-memory database for tests
services.AddDbContext<AppDbContext>(options =>
options.UseInMemoryDatabase("TestDb"));
// Mock external services
services.AddScoped<IEmailService, MockEmailService>();
}
}
# Test Database (in-memory)
TEST_DATABASE_URL=InMemory
USE_IN_MEMORY_DATABASE=true
# Mock OAuth (no real credentials needed)
GOOGLE_CLIENT_ID=test-google-id
GOOGLE_CLIENT_SECRET=test-google-secret
JWT_SECRET=test-jwt-secret-not-for-production
# Test Environment Settings
ASPNETCORE_ENVIRONMENT=Testing
LOGGING_LEVEL=Warning
DISABLE_HTTPS_REDIRECT=true
# Mock External Services
MOCK_EMAIL_SERVICE=true
MOCK_PAYMENT_SERVICE=true
SKIP_EXTERNAL_CALLS=true
[Fact]
public async Task CreateProduct_ShouldSucceed()
{
// DotEnvX automatically loads .env.testing
var factory = new WebApplicationFactory<Program>();
var client = factory.CreateClient();
// Test with configuration from .env.testing
var response = await client.PostAsync("/api/products", content);
response.Assert().IsSuccessStatusCode();
}
Powerful features for enterprise applications and complex deployment scenarios.
Ensure critical variables are present at startup
Validate URLs, connection strings, and patterns
Ensure numeric and boolean values are valid
options.Required = new[] {
"DATABASE_URL",
"JWT_SECRET",
"API_KEY"
};
Follow these guidelines to secure your application configuration effectively.
Use encryption for passwords, API keys, and sensitive URLs
Separate .env files for development, testing, and production
Define required variables to fail fast on missing configuration
Store encryption keys in secure key vaults or environment variables
Rotate encryption keys periodically for enhanced security
Add .env files to .gitignore, except encrypted production files
Never hardcode encryption keys in source code
Keep encryption keys separate from application code
Use different encryption keys for different environments
Always validate critical configuration variables
Start using Noundry.DotEnvX today and manage your application configuration with encryption, validation, and environment-specific support.