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

problem

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

From https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-6.0&tabs=windows:

"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?

solution

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()
    .AddJsonFile("appsettings.json")
    .AddJsonFile("appsettings.Production.json")
    .AddUserSecrets(Assembly.GetExecutingAssembly())
    .Build();

var azureKey = configuration["AzureKey"];
Gravatar
Author
Dan Dumitru
Last Edit
Jul 27, 2022 at 09:00

Your Comment

Feel free to add your improvement suggestions or alternative solutions.
preview
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](http://a.com)

*italic* **bold**

More Tips