liveBook · Manning

Secret

As explained in .NET in Action, Second Edition

A secret is sensitive information, such as a credential or cryptographic key, that must be protected from unauthorized disclosure.

Secrets are sensitive values—such as passwords, cryptographic keys, connection strings, and API keys—that can put systems at risk if exposed. Unlike ordinary environment-specific configuration, they require controlled access. section 13.4

Keep secrets out of appsettings.json and checked-in source. Repository access is a threat vector, and a leaked password can require rotating credentials and cleaning up Git history. section 9.4.2

Provide secrets according to the deployment context:

  • In production, have the deployment pipeline retrieve secrets from a secure store and make them available to the application; Kubernetes provides secrets to pods. section 9.4.2
  • For local development, initialize .NET Secret Manager with dotnet user-secrets init. Its project GUID can be checked in; the referenced secret file stays on the developer’s machine. section 9.4.2
  • Environment variables can supply secrets, including in Azure App Services, but they are no longer recommended for Kubernetes because they are less secure than Kubernetes secrets. section 9.4.2
Listing 9.13 Adds the connection string to Secret Manager for ManningBooksApi project
dotnet user-secrets set "ConnectionStrings:Catalog"
  "DataSource=manningbooks.db;cache=shared"

Store a local connection string with the .NET Secret Manager command rather than placing it in appsettings.json. section 9.4.2

Listing 9.14 Getting connection string from secret through configuration
var builder = WebApplication.CreateBuilder(args);
var config = builder.Configuration;                        #1
 
builder.Services.AddDbContext<CatalogContext>(options =>
{
  var connStr = config.GetConnectionString("Catalog");     #2
  options.UseSqlite(connStr);
});

Read the stored connection string through application configuration when configuring the database context. section 9.4.2

How .NET in Action, Second Edition covers it

How deep it goes
Distinguishes secrets from ordinary configuration and demonstrates local storage and configuration-based retrieval.
Examples
Uses a database connection string, .NET Secret Manager commands, Kubernetes secrets, and Azure App Service environment variables.
What you can do after
Apply it via .NET Secret Manager to store a local connection string and read it through application configuration.
How it connects
Connects secrets to application configuration, continuous deployment, Kubernetes, and environment variables.

Also explained in

Compare how 3 books explain secret →

Related concepts

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