I’m building a .NET app and want to manage settings like API keys, connection strings, and feature flags. Should I use appsettings.json, environment variables, or something else?
Loading
I’m building a .NET app and want to manage settings like API keys, connection strings, and feature flags. Should I use appsettings.json, environment variables, or something else?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Velsamy Ananthavelu81 reputationPosted Oct 8, 2025, 6:35 AM EDT
Yes, you can place the keys, flags, connection string everything in the appsettings.json.
If API Keys are added in the header and validated in the middleware
Prasad RaveendranRank #320 · 7.8K reputationPosted Oct 8, 2025, 9:39 PM EDT
Make sure sensitive information is not stored directly in
appsettings. Instead, keep it in environment variables or a secure service like Azure Key Vault.Sandhiya PriyaRank #26 · 43.2K reputationPosted Oct 13, 2025, 2:49 AM EDT
In a .NET app, managing settings depends on security, environment, and ease of change.
Here’s a clear comparison:
1. appsettings.json
What it is: A JSON file that stores configuration like connection strings, API keys, and feature flags.
Pros:
Easy to read and edit.
Supports nested configurations.
Works well with
.NET Configuration API.Cons:
Not secure for secrets (like API keys) if stored in source control.
You need separate files for each environment (
appsettings.Development.json,appsettings.Production.json).Example:
{ "ConnectionStrings": { "DefaultConnection": "Server=.;Database=MyDB;Trusted_Connection=True;" }, "ApiKeys": { "GoogleMaps": "YOUR_API_KEY" }, "FeatureFlags": { "NewUI": true } }2. Environment Variables
What it is: Settings stored in the OS environment, outside the app.
Pros:
Great for secrets (API keys, passwords).
Can vary per environment without changing code.
Works well in cloud deployments (Azure, AWS, Docker).
Cons:
Harder to manage if there are many settings.
Not version-controlled.
Example in .NET:
3. User Secrets (for Development)
What it is: A secure storage for secrets during local development.
Pros:
Keeps secrets out of source control.
Integrated with .NET Core (
dotnet user-secrets).Cons:
Only for development, not production.
4. Azure Key Vault or Other Secret Managers
What it is: A secure, centralized storage for secrets.
Pros:
Highly secure for production.
Works with CI/CD pipelines.
Cons:
Adds complexity.
Requires additional configuration.
Best Practice
General settings / feature flags: Use
appsettings.jsonorappsettings.{Environment}.json.Secrets (API keys, passwords, tokens): Use environment variables or Azure Key Vault.
Combine: Use configuration providers in .NET Core to merge
appsettings.json, environment variables, and secret managers seamlessly.Example in Program.cs (.NET 6+):
var builder = WebApplication.CreateBuilder(args); builder.Configuration .AddJsonFile("appsettings.json") .AddEnvironmentVariables(); // Environment variables override JSON var app = builder.Build();