liveBook · Manning

AddUserSecrets

As explained in ASP.NET Core in Action, 3e

AddUserSecrets is a .NET configuration extension method that loads User Secrets for an application.

AddUserSecrets loads User Secrets into ASP.NET Core configuration during local development, keeping sensitive values outside the project folder and source control. section 10.2.3

Use the extension with these practical safeguards:

  • Register it only when the environment is Development; production should use environment variables or Azure Key Vault. section 10.2.3
  • Call AddUserSecrets<Program>(); Program identifies the assembly containing the UserSecretsId project property. section 10.2.3
  • Initialize User Secrets and add values with the .NET CLI, or edit the external secrets.json file directly. section 10.2.3

Load secrets only during development

if (builder.Environment.IsDevelopment()) { builder.Configuration.AddUserSecrets<Program>(); } The provider uses the UserSecretsId from the project file to locate secrets.json. The default WebApplicationBuilder configures this behavior correctly by default. section 10.2.3

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains conditional registration, generic assembly marking, UserSecretsId lookup, and secrets.json path generation.
Examples
Uses a conditional AddUserSecrets<Program>() registration, UserSecretsId project metadata, and CLI secret-management commands.
What it assumes
Assumes an ASP.NET Core application and project file, while explaining UserSecretsId and the development environment check.
What you can do after
Configure local development secrets safely through ASP.NET Core configuration and the .NET CLI.
How it connects
Connects User Secrets with ASP.NET Core configuration, environment variables, Azure Key Vault, and the .NET CLI.

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.