Skip to content

How To - Manage User Secrets In Asp.net Core

Once initialized, secrets are stored in a secrets.json file located in your user profile folder (e.g., %APPDATA%\Microsoft\UserSecrets\ on Windows or ~/.microsoft/usersecrets/ on macOS/Linux).

public class MyController : ControllerBase { private readonly IConfiguration _config; public MyController(IConfiguration config) => _config = config; public IActionResult Get() { var apiKey = _config["ServiceApiKey"]; // Retrieves the secret return Ok(); } } ``` ### Key Best Practices * **Development Only:** User secrets are **not encrypted** and are only intended for local development. * **Production Security:** Never use Secret Manager for production. Instead, use more secure providers like [Azure Key Vault](https://learn.microsoft.com/en-us/aspnet/core/security/key-vault-configuration) or [environment variables](https://microsoft.com). * **Source Control:** Ensure your `secrets.json` file path is never added to `.gitignore`, though it should already be safe since it lives outside the project folder. Use code with caution. Copied to clipboard How to manage user secrets in ASP.NET Core - InfoWorld How to manage user secrets in ASP.NET Core

dotnet user-secrets set "ServiceApiKey" "12345" ``` To group secrets (e.g., for a "Movies" section), use a colon: ```bash dotnet user-secrets set "Movies:ServiceApiKey" "12345" ``` Use code with caution. Copied to clipboard 3. Access Secrets in Code Once initialized, secrets are stored in a secrets

To use user secrets, you must first initialize your project. This adds a UserSecretsId to your .csproj file, which maps your project to a specific folder in your local user profile. Instead, use more secure providers like [Azure Key

Join our flagship global event, Sustain 2026, in Paris & Online on Mar 2-3!
Register now
New: 5 Key Accelerators of Leading Sustainable Procurement Programs
View Now
New: A Four-Step Blueprint for a More Resilient Supply Chain
View Now
Just released: The Global Supply Chain Sustainability Risk & Performance Index
View now