Skip to content
Menu
Archicode
  • About me
Archicode
August 3, 2020

Safely store secrets during development

When developing an application, we sometimes need to call an api or connect to a database and for this, we use passwords or keys to connect. If we put them in the appsettings, there’s always the danger of forgetting about them and commit them to source control which can then cause a security flaw.

To prevent this, dotnet provides us with a very useful command to safely store our secrets where they won’t be accidentally committed.

  1. First open a command line in your solution folder
  2. Execute this command :
    dotnet user-secrets init
    This command initialises the local secret repository of your solution
  3. To store data, use this command
    dotnet user-secrets set "keyname" "keyvalue"

Now we can easily access it from our Console application without risking this secret to leak or end up in the wrong hands.

  1. You need to install the following nuget Packages into your solution
    Microsoft.Extension.Configuration
    Microsoft.Extension.Configuration.UserSecrets
  2. In your Program.cs file you will need a ConfigurationBuilder and tie the Id you received from the init command you ran before to the builder
    var builder = new ConfigurationBuilder();
    builder.AddUserSecrets("511ae28b-c9d3-4e21-979f-a310df6102ad");
  3. After a call to the buillder.Build() method you can now access your secret
    var configuration = builder.Build();
    var secret = configuration["Secret"];

Happy coding

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How to Deactivate and Reactivate a Plugin in Microsoft Dynamics 365: Resolving the “ProjectOperations_Anchor PackageImportFailed” Error
  • Issue when debugging Azure Function locally
  • Understanding Blazor Component Parameters
  • Error NETSDK1082 in Maui Blazor Hybrid App
  • Understanding @inject in Blazor Components

Recent Comments

  1. Harisinou on Understanding Blazor Component Parameters
  2. ปั้มไลค์ on Encourage your kids to read books
  3. Jamesglalt on How to : Blazor components (basic)
  4. Liza Conable on How to blazorify an existing mvc app
  5. BrianKit on How to : Blazor components (basic)

Archives

  • April 2025
  • May 2024
  • December 2023
  • November 2023
  • October 2023
  • May 2023
  • March 2021
  • August 2020
  • July 2020
  • June 2020
  • October 2019
  • June 2019
  • February 2019

Categories

  • .NET
  • Fixes
  • IoT
  • Misc
  • Uncategorised
  • Uncategorized
©2025 Archicode | Powered by WordPress and Superb Themes!