Environment variables as configuration store
How you can use Environmet variables as secure store for your application configuration?
ASP.NET / .NET 8 / .NET 9 Freehosting
If you don't already have our ASP.NET / .NET Core Freehosting, sign up for FREE at https://MonsterASP.net/.
Description
In ASP.NET Core application is useful storing configuration values in Environment variables because it makes your application more secure, flexible and easier to deploy. Environment variables allow you to separate application configuration from your code. Also same application can run in development, testing and production without changing appsettings.json.
Environment variables are especially good for storing sensitive data as:
- connection strings
- API keys
- passwords
These values are not stored in source code, Git or in file on website disk space, which reduces security risks.
Environment variables also make it easy to change settings without rebuilding or redeploying application.
You can update Environment value directly in Control panel and your application will load/use it after restart.
How to write Environment values?
There is simple appsettings.json example:
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=MyDb;User Id=sa;Password=localpass;"
},
"Mail": {
"Host": "smtp.local",
"Port": 25,
"Username": "test",
"Password": "test123"
}
}
ASP.NET uses double underscores (__) as replacement for JSON nesting when reading configuration from Environment variables.
For example Mail__Host maps to Mail:Host in appsettings.json file.
There are values from example appsettings.json file writen as Environment variables:
| Key | Value |
|---|---|
| ConnectionStrings__DefaultConnection | Server=prodserver;Database=proddb;User Id=produser;Password=prodpass; |
| Mail__Host | smtp.prod |
| Mail__Port | 25 |
| Mail__Username | produser |
| Mail__Password | prodpass |
Manage Environment variables in Control panel
Control panel --> Websites --> Manage website --> Scripting --> Environment Variables

Preview loaded configuration value
This is preview of simple ASP.NET Core application demonstrating which values from appsettings.json are loaded at runtime.
