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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sandhiya PriyaPosted Oct 13, 2025, 6:49 AM
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:
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+):
Prasad RaveendranPosted Oct 9, 2025, 1:39 AM
Make sure sensitive information is not stored directly in
appsettings. Instead, keep it in environment variables or a secure service like Azure Key Vault.Velsamy AnanthaveluPosted Oct 8, 2025, 10:35 AM
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