IC Improve Code Recipes

Chapter 6

Configuration and Secrets

Configuration looks like the easiest part of an application, and for that reason nobody designs it. A key gets added to web.config when it's needed, read with ConfigurationManager.AppSettings wherever it's needed, and parsed with int.Parse on the spot. Ten years later there are 140 keys, nobody knows which ones are still read, and a typo in one of them surfaces as a NullReferenceException at 4 P.M. on the first day of the month, when the monthly job finally runs.

In my archive, about 10,099 hand-written C# files read ConfigurationManager.AppSettings directly, across 27 of the 35 engagements with C# code. Compare that with the configuration classes: only 528 of them, in 21 engagements. Teams wrote thousands of logging and data helpers but almost never wrapped their settings in a type. The settings were simply read, as strings, from everywhere.

Secrets are the sharper version of the same problem. About 6,259 files contain text that looks like a hard-coded password (Password= or Pwd=), in 28 engagements, and about 646 files contain a complete hard-coded connection string, in 24 engagements. Every one of those passwords is in source control, in every branch copy and in every backup of the repository.

This chapter moves configuration to IConfiguration and strongly typed options that are validated at startup, sets up environments without web.config transforms, and gets secrets out of source code and into user secrets, environment variables or Azure Key Vault.

Recipes

  1. 6.1From ConfigurationManager.AppSettings to IConfigurationSettings are read as strings, deep inside business code, at the moment they are used. Missing or malformed values fail late and far from the cause.…
  2. 6.2The Options Pattern with ValidationTyped settings help, but nothing checks them. A missing API key or a timeout of zero is only discovered when the first request that needs it fails,…
  3. 6.3Environments Without web.config TransformsEach environment needs different settings: a local database for developers, a test server for QA, production values in production. The legacy…
  4. 6.4User Secrets, Environment Variables and Azure Key VaultDevelopers need real credentials to run the app locally, so they paste them into appsettings.json and hope to remember not to commit them. Servers…
  5. 6.5Getting Connection Strings Out of Source CodeConnection strings with SQL logins and passwords are compiled into classes, pasted into console tools and copied between projects. Some of them point…

Chapter checklist

  • Search for ConfigurationManager.AppSettings and list every key the application reads; delete the ones nothing reads.
  • Group settings into sections with typed classes, and read configuration only in Program.cs or the composition root.
  • Register options with ValidateDataAnnotations() and ValidateOnStart() so bad configuration fails the deployment, not the first request.
  • Replace web.config transforms with appsettings.{Environment}.json files that contain no secrets, and build once for every environment.
  • Log the environment name and configuration sources at startup, never the values.
  • Move every secret to user secrets, environment variables or Key Vault, and rotate any password that was ever committed.
  • Prefer integrated security or managed identity for SQL Server, and add a startup guard against passwords in JSON files.