ASP.NET Load user secrets.json when environment is not Development


In an ASP.NET project on .NET 6 the user secrets are only loaded when the environment used is Development.


"WebApplication.CreateBuilder initializes a new instance of the WebApplicationBuilder class with preconfigured defaults. The initialized WebApplicationBuilder (builder) provides default configuration and calls AddUserSecrets when the EnvironmentName is Development."

Sometimes you need to run the project locally using a different environment, like Production, or anything different from Development. By default, the user secrets are not loaded in this situation. What should you do to load them?


You can explicitly build the configuration again, including the appsettings file for your environment, before the code that needs the values from it, and call the AddUserSecrets method. This is how the code would look like for the Production environment:

var configuration = new ConfigurationBuilder()

var azureKey = configuration["AzureKey"];
Author: Dan Dumitru
Last Edit: July 27, 2022
Last Edit July 27, 2022
Created July 27, 2022

Pay it forward!

Did this solution help you?
You can pay it forward and write a solution here yourself, when you have one, helping future Googlers like you... >> read more

Your Comment

Feel free to post additional info or improvement suggestions.
Optional, never shown, displays gravatar.

Formatting Tips

This editor uses Markdown to easily add code in your posts.

Triple backticks for full line(s) of code (or indent 4 spaces)

let foo = 'bar';

[link text](

*italic* **bold**

More Tips