User Secrets
Securely store development secrets outside of your codebase.
User Secrets keep sensitive development configuration out of source control.
Initialize User Secrets
bash
cd AuthScape.APIdotnet user-secrets init
This adds a UserSecretsId to your .csproj:
xml
<PropertyGroup><UserSecretsId>your-guid-here</UserSecretsId></PropertyGroup>
Setting Secrets
bash
# Set individual secretsdotnet user-secrets set "ConnectionStrings:DefaultConnection" "Server=localhost;..."dotnet user-secrets set "AppSettings:Stripe:SecretKey" "sk_test_xxx"dotnet user-secrets set "AppSettings:SendGrid:ApiKey" "SG.xxx"
Listing Secrets
bash
dotnet user-secrets list
Removing Secrets
bash
# Remove single secretdotnet user-secrets remove "AppSettings:Stripe:SecretKey"# Clear all secretsdotnet user-secrets clear
Secrets Location
Secrets are stored outside the project:
- Windows:
%APPDATA%\Microsoft\UserSecrets\<user_secrets_id>\secrets.json - Linux/Mac:
~/.microsoft/usersecrets/<user_secrets_id>/secrets.json
secrets.json Format
json
{"ConnectionStrings:DefaultConnection": "Server=localhost;Database=AuthScape;...","AppSettings:Stripe:SecretKey": "sk_test_xxx","AppSettings:SendGrid:ApiKey": "SG.xxx"}
Loading in Program.cs
User secrets are loaded automatically in Development:
csharp
var builder = WebApplication.CreateBuilder(args);// User secrets loaded automatically when:// - Environment is Development// - UserSecretsId is set in .csproj
Best Practices
- Never commit secrets - Add
secrets.jsonto.gitignore - Use for development only - Use Azure Key Vault or AWS Secrets Manager in production
- Document required secrets - Maintain a template of required keys