In order to avoid storing sensitive data into source control, we have User Secrets that can be used for development and Environment that can be used for production.
I'm trying to set up my project correctly. On my development machine, I want to be able to switch between Development and Production configuration values, and for Integration Tests, it must get access to Development configuration.
Currently, the Integration Tests configuration is hard-coded in the class library test project, and repeated in the appsettings.Development.json file, and I avoided creating appsettings.Production.json. That's not ideal.
Normally, User Secrets should be used for Development and Environment Variables for Production. If I set both, then Production configuration defined in Environment Variables will always overwrite the Development values. That won't work.
The other solution is creating appsettings.Staging.json with production values, and excluding that file from source control.
Another problem. Applying User Secrets and Environment variables on a class library won't be read by default. Only the configuration defined on the Web project is being read. Also, the Integration Tests project won't be able to access the configuration defined on the Web project.
This leads to 2 additional problems:
First, if I have 3 different Web projects that rely on the same class library that requires an API key, the API key needs to be configured 3 times.
Second, the Integration Test cannot use any of those configurations and needs to repeat the configuration a 4th time.
What would be the best way to configure the project? It's not looking like Host.CreateDefaultBuilder
will provide an ideal solution. Creating a custom configuration builder might be the way to go, but before heading into that direction, I'd appreciate some input.
User Secrets are nice but they don't offer a way of switching them; and not using User Secrets means that all API keys and passwords are stored in plain text in source control. Perhaps the solution will be to force reading User Secrets for the class libraries, and depending on ASPNETCORE_ENVIRONMENT, read Secrets Manager for Development and Environment Variables for Staging or Production, but I don't know how to configure it in such a way.
appsettings.Development.json
/appsettings.Staging.json
files. – Etienne Charland