liveBook · Manning

Secrets.json

As explained in Building Web APIs with ASP.NET Core

secrets.json is the local user-secrets configuration file that stores development-only key-value settings outside the project directory.

secrets.json holds private, development-specific settings for an ASP.NET Core application. It lives outside the project directory, so a developer can keep a local database connection string out of the project’s appsettings.json files. section 4.3.4

To set it up for the board-game API:

  • In Visual Studio, right-click the project’s root folder and choose Manage User Secrets. Visual Studio adds a UserSecretsId to the project file, creates secrets.json under the matching local UserSecrets folder, and opens it for editing. section 4.3.4
  • Add ConnectionStrings:DefaultConnection with the development database connection string. Replace the sample password with the password for your database login. section 4.3.4
{
 "ConnectionStrings": {
   "DefaultConnection": "Server=localhost\\SQLEXPRESS;Database=MyBGList;
       User Id=MyBGList;Password=MyS3cretP4$$;
       Integrated Security=False;MultipleActiveResultSets=True;
       TrustServerCertificate=True"
 }
}

This JSON uses the ConnectionStrings and DefaultConnection keys expected by GetConnectionString("DefaultConnection") in Program.cs. Use your own database credentials rather than the sample password. section 4.3.4

Keys in secrets.json override matching appsettings*.json keys and add keys that are absent. The API can therefore retrieve DefaultConnection through its usual configuration call. For production, create appsettings.Production.json on the server with the corresponding keys and production values; do not add that credentials file to the shared project. section 4.3.4 section 12.1.4 section 12.3.7

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Walks through Visual Studio setup, file location, JSON keys, and configuration lookup; leaves production credentials to server-side configuration.
Examples
Uses a SQL Server DefaultConnection value stored under ConnectionStrings and retrieved when configuring ApplicationDbContext.
What it assumes
Assumes a Visual Studio ASP.NET Core web API project and teaches the user-secrets file setup from the context menu.
What you can do after
Operate/configure it through Visual Studio User Secrets so the API reads local database credentials without placing them in appsettings.json.
How it connects
Connects UserSecretsId, appsettings.json overrides, GetConnectionString, and appsettings.Production.json.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.