The .NET Secret Manager: UserSecretsId in the project file, dotnet user-secrets init/set/list, the secrets.json location on Windows, Linux and macOS, loading only in the Development environment, why it is plain text outside the repo rather than encryption, and production replacements such as Azure Key Vault with managed identity and platform secret stores.
User secrets (the Secret Manager) keep developer-only secrets out of the repository. dotnet user-secrets init adds a UserSecretsId to the project file, and dotnet user-secrets set "Stripe:ApiKey" "sk_test_..." writes the value to a plain-text secrets.json in your user profile (%APPDATA%\Microsoft\UserSecrets\<id> on Windows, ~/.microsoft/usersecrets/<id> on Linux and macOS).
CreateBuilder loads them only in the Development environment, as a provider after the appsettings files, so code reads them like any other key.
They are not encryption: anyone or anything running as you can read the file. In production use Azure Key Vault with managed identity, Key Vault references, or platform secrets, and passwordless Entra ID authentication where the service supports it. The trade-off: zero setup for developers, but no protection beyond "not in git".
"User secrets solve one problem: stopping developers from committing their local API keys and connection strings. When I run
dotnet user-secrets init, the project file gets aUserSecretsIdGUID.dotnet user-secrets setthen writes key-value pairs into asecrets.jsonfile in my user profile, under that GUID, outside the source tree.WebApplication.CreateBuilderadds a user secrets provider, but only when the environment is Development, and it sits afterappsettings.jsonandappsettings.Development.json, so a secret overrides a placeholder in the JSON. My code just readsStripe:ApiKeyfrom configuration and doesn't know where it came from.The important thing is what it is not. It isn't encrypted and it isn't a vault: the file is plain JSON readable by any process running as me. It also doesn't exist on a server, which is exactly why it's Development-only. In production I use Azure Key Vault, loaded with
AddAzureKeyVaultand a managed identity, or Key Vault references in App Service and Container Apps that surface secrets as environment variables. Better still, for Azure SQL, Storage or Service Bus I use Entra ID authentication with the managed identity, so there's no secret to store at all.The mistake I see is people treating user secrets as secure storage, copying
secrets.jsonaround on chat, or setting the environment to Development on a test server just so it reads secrets. User secrets keep secrets out of git; they don't keep them secret."
| Command | Effect |
|---|---|
dotnet user-secrets init |
Adds <UserSecretsId> to the .csproj |
dotnet user-secrets set "Stripe:ApiKey" "sk_test_123" |
Adds or updates a key |
dotnet user-secrets list |
Prints all values (in plain text) |
dotnet user-secrets remove "Stripe:ApiKey" / clear |
Deletes one / all |
cat secrets.json \| dotnet user-secrets set |
Bulk import a JSON file |
| OS | Path |
|---|---|
| Windows | %APPDATA%\Microsoft\UserSecrets\<UserSecretsId>\secrets.json |
| Linux / macOS | ~/.microsoft/usersecrets/<UserSecretsId>/secrets.json |
| Option | Protection | Where it fits |
|---|---|---|
| User secrets | Outside git only; plain text | Developer machines |
| Environment variables from the platform | Visible to anyone with portal or kubectl access |
Non-critical values, or as the delivery of Key Vault references |
| Azure Key Vault + managed identity | Encrypted at rest, RBAC, audit log, rotation | Production secrets |
| Entra ID (passwordless) auth | No secret exists | Azure SQL, Storage, Service Bus, Key Vault itself |
On a team of [eight] developers, each had their own Stripe test key and a local SQL password in user secrets, with placeholders like "ApiKey": "set-in-user-secrets" in appsettings.json so missing values were obvious. In Azure Australia East the same keys came from Key Vault through the app's managed identity, and the database connection used Authentication=Active Directory Managed Identity, so production had no SQL password at all.
Checks that the candidate understands what user secrets protect against and what they don't.
Green flags
UserSecretsId and the CLI commands.Red flags
The UserSecretsId in the project file is compiled into the assembly as [assembly: UserSecretsIdAttribute("...")] by the Microsoft.Extensions.Configuration.UserSecrets build targets. At run time, AddUserSecrets(assembly) reads that attribute and builds the path:
.csproj <UserSecretsId>7f1c…</UserSecretsId>
│ build
▼
assembly attribute UserSecretsIdAttribute("7f1c…")
│ run (Development only)
▼
PathHelper.GetSecretsPathFromSecretsId("7f1c…")
└─► ~/.microsoft/usersecrets/7f1c…/secrets.json (JSON provider, reloadOnChange)
CreateBuilder calls AddUserSecrets with the entry assembly when IsDevelopment() is true and the attribute exists.
dotnet user-secrets set writes flattened keys:
{ "Stripe:ApiKey": "sk_test_123", "ConnectionStrings:Default": "Server=..." }
Nested JSON also works if you edit the file by hand (Visual Studio's Manage User Secrets opens it).
There is no encryption and no access control beyond the file system. Malware, a browser extension or a sync tool running as you can read it. It also travels in profile backups. Its only guarantee is that the secret is not in the repository.
Console apps and tests can call new ConfigurationBuilder().AddUserSecrets<Program>(). Integration tests sometimes use it for local connection strings; CI should use pipeline secrets instead.
Developer laptop Azure (Australia East)
──────────────── ───────────────────────
secrets.json Key Vault ──RBAC──► managed identity of the app
│ │
▼ ▼
IConfiguration["Stripe:ApiKey"] (same key, different provider)
Key Vault secret names can't contain :, so the Key Vault provider maps -- to : (Stripe--ApiKey).
dotnet user-secrets init in a web project and open the .csproj to find the GUID.secrets.json in a text editor and confirm it is plain text.ASPNETCORE_ENVIRONMENT=Production and confirm the secret is no longer read.appsettings.json and confirm the secret overrides it in Development.GetDebugView() to see the key reported against the user secrets JSON provider.Local development with user secrets, production with Key Vault, and the same key read by code in both cases.
cd src/Shop.Api
dotnet user-secrets init
dotnet user-secrets set "Stripe:ApiKey" "sk_test_local_only"
dotnet user-secrets list
# Production: grant the app's managed identity read access to secrets only (least privilege)
az role assignment create --role "Key Vault Secrets User" \
--assignee <managed-identity-principal-id> \
--scope /subscriptions/<sub>/resourceGroups/<rg>/providers/Microsoft.KeyVault/vaults/<vault>
// Program.cs
using Azure.Identity;
var builder = WebApplication.CreateBuilder(args); // loads user secrets automatically in Development
if (!builder.Environment.IsDevelopment())
{
// Production: secrets from Key Vault through the app's managed identity, no secret needed to read secrets.
// The identity needs only the "Key Vault Secrets User" role on this vault (RBAC permission model).
var vaultUri = new Uri(builder.Configuration["KeyVault:Uri"]!);
builder.Configuration.AddAzureKeyVault(vaultUri, new ManagedIdentityCredential());
}
builder.Services.AddOptions<StripeOptions>()
.BindConfiguration(StripeOptions.Section)
.ValidateDataAnnotations()
.ValidateOnStart(); // fail at start-up if no provider supplied the key
var app = builder.Build();
app.Run();
public sealed class StripeOptions
{
public const string Section = "Stripe";
[System.ComponentModel.DataAnnotations.Required]
public string ApiKey { get; init; } = "";
}
secrets.json, so people work around it by committing values or writing the file in a script that leaks into logs. Instead: Use the pipeline's secret store (GitHub Actions secrets, Azure DevOps variable groups linked to Key Vault) delivered as environment variables, or workload identity federation so no secret exists.secrets.json through chat or email to onboard people. Why it is wrong: The secret now lives in more systems with weaker controls. Instead: Give each developer their own test credentials, or a shared dev Key Vault they access with their own identity.dotnet user-secrets init on a new project and wondering why nothing loads. Why it is wrong: Without UserSecretsId there is no file to read. Instead: Initialise once and commit the .csproj change.appsettings.Development.json because "it's only dev". Why it is wrong: That file is committed. Instead: Commit placeholders and keep values in user secrets.User secrets are a convenience for keeping values out of git, not secure storage, so I treat them as the last resort rather than the default. Even the best secret store is second best to no secret. For Azure resources I push for managed identity with Entra ID auth (Azure SQL, Storage, Service Bus, Key Vault), and developers authenticate the same code with their own login through DefaultAzureCredential locally. What remains for user secrets is whatever cannot use identity, typically personal test keys for third-party APIs; anything shared by the team goes in a dev Key Vault that each developer reads with their own identity, so access is audited and revocable. Under the Privacy Act and the ACSC Essential Eight, fewer long-lived credentials means fewer things to rotate and fewer to leak, and a lost laptop holds nothing that unlocks production.
| Option | When it fits | Cost | When not to |
|---|---|---|---|
| User secrets | Individual developer test keys | None | Shared or production secrets |
| Shared dev Key Vault + developer identity | Team-wide dev secrets | Azure setup and RBAC | Offline development |
| Managed identity, passwordless | Azure services in every environment | Initial RBAC wiring | Third-party APIs that only accept keys |
Properties/launchSettings.json defines launch profiles for local development: how to start the app, which URLs, and which environment variables. Visual Studio, Rider, VS Code with C# Dev Kit and dotnet run all read it, but it is not published. In this file I would flag: secrets are committed (a production connection string and API key), so they must be rotated now and moved to user secrets; the environment is Production, which switches on production behaviour and turns off user secrets, and combined with the production connection string means a developer's F5 talks to the live database; and it binds to 0.0.0.0 over plain HTTP, exposing the app to the network. I would replace it with HTTPS and HTTP profiles on localhost using Development, and add a separate production-like profile only if someone needs to test production behaviour against non-production resources.
I'd inventory and classify every setting first: secrets, environment-specific values, shared values, and which must change live. Secrets go to Key Vault. Non-secret settings and feature flags go to Azure App Configuration with a label per environment, and secrets are referenced from there through Key Vault references. The app authenticates with its managed identity using least-privilege roles, and where possible secrets disappear entirely through passwordless Entra ID auth to SQL, Storage and Service Bus.
For refresh, use a sentinel key with a refresh interval and a secret refresh interval, and read live values through IOptionsMonitor. Rotation uses overlapping versions, so old and new both work during the window. Add start-up timeouts and replicas, and keep ValidateOnStart.
The trade-off: central, audited, rotatable configuration, in exchange for a network dependency at start-up that you must design for.
Last updated 23 Nov 2026.