Right now our connection strings sit in appsettings.json as plain text, and I'm worried about secrets leaking into source control or container images. We're not ready to adopt Azure Key Vault because the team wants to keep dependencies light.
What's the most practical approach for a small ASP.NET Core API using Dapper? I've heard about User Secrets for local development and environment variables for staging, but I'm unsure how to wire that cleanly into IConfiguration without scattering GetConnectionString calls everywhere. Is there a standard helper or pattern you'd recommend?
For a small Dapper API, keep connection string configuration simple and keep database connections short-lived. Store the string in configuration; create a fresh
SqlConnectionfor each operation and dispose of it when the operation finishes.Keep the string in configuration
Use a named entry such as
ConnectionStrings:ApiDatabase. A localappsettings.Development.jsoncan hold non-sensitive development settings, while passwords and production credentials should come from .NET User Secrets or a deployment secret store. Environment variables can override the setting with the nameConnectionStrings__ApiDatabase. Don’t commit real credentials or log the connection string.Use a small connection factory
A singleton factory is fine because it stores only the immutable string. It should create a new connection each time; don’t register one open
SqlConnectionfor the whole application. SQL Server’s built-in connection pooling makes opening and disposing connections per operation practical.This keeps configuration separate from connection lifetime without adding much machinery. If the API grows, the same factory can be replaced or extended without changing how repositories obtain connections.