Skip to content
tezvyn:

How do you manage environment-specific connection strings and why is hardcoding bad?

Source: learn.microsoft.comEasyHow cards are made

Tests config separation and secrets hygiene. Outline: inject via environment variables or a secret store, keep per-environment configs credential-free, and prefer managed identities.

What's really being asked

This question evaluates whether you treat configuration as separate from code and whether you understand the operational and security risks of embedding secrets in an application binary. A senior candidate should demonstrate familiarity with environment-based injection, secret stores, and platform-specific authentication patterns rather than manual credential juggling.

The full answer

Four things in order. First, the mechanism for environment-specific values: environment variables, a secret store such as Azure Key Vault or AWS Secrets Manager, or encrypted configuration files like appsettings.Production.json that are deployed by the pipeline and never committed to source control. Second, the security argument against hardcoding: strings compiled into assemblies can be read using ILDASM to view CIL, and any secret checked into git remains in history even after deletion. Third, runtime protection: setting Persist Security Info=False so that credentials are discarded after the connection opens, and encrypting configuration files at rest. Fourth, authentication best practices: preferring password-less methods such as Managed Identities for Azure SQL or Windows Authentication with integrated security, eliminating passwords from the string entirely.

The mistakes people make

Saying you would use a switch statement or preprocessor directives to pick the string per environment, which still embeds secrets in code. Storing plaintext passwords in appsettings files and checking them into Git. Proposing Universal Data Link files, which are stored in clear text and cannot be encrypted using .NET. Claiming that staging credentials do not matter because the data is not production; attackers routinely pivot from staging to production.

What usually comes next

How would you rotate a compromised connection string without redeploying? How do you handle local developer machines that cannot reach the corporate secret store? What is the blast radius if the CI/CD pipeline itself is compromised and has vault read access? When is it acceptable to use a connection string builder, and how do you prevent connection string injection from user input?

A concrete example

In a .NET application deployed to Azure, you would store the connection string as a secret in Azure Key Vault, reference it via the Azure Key Vault configuration provider, and use a Managed Identity so the connection string contains no password. For local development, you use the ASP.NET Core Secret Manager tool or a developer-specific Key Vault access policy so that appsettings.Development.json only contains non-secret overrides. The pipeline injects the vault URI via an environment variable, and the runtime resolves the secret during startup.

Interview question

When configuring a .NET app for multiple environments, which approach best prevents connection string secrets from being exposed?

  • a.Place the connection string in a Universal Data Link file on the server and reference it from the application configuration
  • b.Inject values via environment variables or a secret store at runtime, and prefer authentication methods that eliminate passwords from the stringCorrect
  • c.Store environment-specific connection strings in appsettings files and check them into source control so they deploy with the application
  • d.Use C# preprocessor directives to select the correct hardcoded string at compile time based on the target environment
Why?

Injecting via environment variables or a secret store keeps secrets out of source control and compiled binaries, while managed identities remove passwords entirely. Preprocessor directives are a tempting distractor because they appear to separate environments but still embed secrets in the assembly that can be extracted with ILDASM.

Just read this? Test yourself on what you have been reading.

Read the original → learn.microsoft.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on ci/cd — each one lists the topics its interview covers.

See open roles