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"];
Dan Dumitru
Last Edit
Jul 27, 2022 at 09:00

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