Skip to content

adam-marianowski/UserSecretsInDotnetConsole

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Adding required packages

dotnet add package Microsoft.Extensions.Configuration
dotnet add package Microsoft.Extensions.Hosting

Configuration

To use secrets the following must be added to the Program.cs:

var host = Host.CreateDefaultBuilder(args).ConfigureAppConfiguration((hostContext, config) => {
    config
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddUserSecrets<Program>(optional: true, reloadOnChange: true);

}).Build();

and to load the secrets:

var configuration = host.Services.GetRequiredService<IConfiguration>();
var value = configuration["Secret"];

When running this command:

dotnet user-secrets init

secrets.json hidden file is created in the following directory:

`%APPDATA%\RoamingMicrosoft\UserSecrets\<USER_SECRETS_ID>`

this allows editing secrets directly in secrets.json. When running or building project the secrets are taken from user-secrets, if that is not available, then appsettings.json secrets are being used.

using user-secrets

All secrets with values can be listed with the following command:

dotnet user-secrets list

and secret can be set via:

dotnet user-secrets set "<secret_name>" "<secret_value>"

Additional documentation

Microsoft Learn Documentation - app-secrets

About

Using user-secrets in .NET console app

Topics

Resources

Stars

Watchers

Forks

Languages