ASP.NET – How to add User Secrets

To add User Secrets in an ASP.NET project in Visual Studio do the following steps:

  • Right-click the project and click Manage User Secrets.
  • This will create and open a file called secrets.json.
  • Override appsettings.json settings by putting them in secrets.json

For example, let’s say your appsettings.json looks like this:

{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*", "ApiKey": "Prod1234" }

You want to override ApiKey. In secrets.json you would put this:

{ "ApiKey": "Dev1234" }

Now when you run your web API, when you go to use the “ApiKey” from the configuration, it’ll use the value from secrets.json instead of appsettings.json.

Full example – Adding User Secrets and passing the values to a controller

Let’s say you’re starting a new project and you need to add Config class that holds two values: ApiKey and a ConnectionString. You need to pass this Config to your controllers, and you want to add User Secrets to override these values in your dev environment.

In this example, I’ll show how to wire everything up. In the end, you’ll see that your User Secrets are being used.

1 – Add a Config class

This class simply holds the configuration values that you want to pass into the controllers.

public class Config { public string ApiKey { get; set; } public string ConnectionString { get; set; } }

2 – In Startup.ConfigureServices register the Config object and read the values from Configuration

In Startup.ConfigureServices, initialize the Config object.

Notice that there’s nothing special about how this is reading from the Configuration object. The ASP.NET framework is already wired up to look for User Secrets and does everything behind the scenes for you.

public class Startup { //other Startup methods public void ConfigureServices(IServiceCollection services) { //other stuff services.AddSingleton<Config>(new Config() { ConnectionString = Configuration.GetConnectionString("DefaultDB"), ApiKey = Configuration["ApiKey"] }); } }

3 – Add a Controller class and dependency inject the Config class

Add the Config class as a constructor parameter in a controller. Because you registered the Config object in Startup.ConfigureServices in the previous step, whenever this controller’s constructor is called, it’ll pass in the Config object.

This is an example controller, and the Get() method below is clearly not realistic, and is only meant to show that the values from the User Secrets file got passed in.

[ApiController] [Route("[controller]")] public class ExampleController : ControllerBase { private readonly Config Config; public ExampleController(Config config) { Config = config; } [HttpGet] public string Get() { if (Config.ApiKey == "Dev1234") { return "It's using the user secrets!"; } else { return "Something went wrong, it's not using the user secrets" } } }

When you run this, you’ll see it return:

It's using the user secrets!

Leave a Comment