Click here to Skip to main content
15,887,746 members
Articles / Programming Languages / C#
Tip/Trick

Visual Studio - User Secrets

Rate me:
Please Sign up or sign in to vote.
4.84/5 (14 votes)
4 Nov 2022CPOL3 min read 15.6K   19   5
Provides a developer’s overview on how to get started with using Visual Studio’s User Secrets
This document provides an overview for developers who wish to quickly get up to speed on using Visual Studio’s User Secrets to store sensitive information on their local environment and not to store it in the code repository.

Purpose

I’d like to take a lot of the legwork out for developers, when investigating a new feature like User Secrets.

Covering relevant topics such as overriding appsettings.json files, loading the appropriate appsetting based on the environment (Development for e.g.), how to add\update your secrets.

Scope

The scope of this document is to convey the tasks required to secure sensitive information locally and not within a team code repository (for e.g., a connection string to your LocalDB, which shows your password).

Prerequisites

Why use User Secrets – What Problem Does it Solve

Usually, the settings are placed in the appsettings.json file, but there are settings that we don’t want to share in this file, like passwords, usernames or fully qualified connection strings.

So, the user secrets are used for that, when we only want secrets to be visible to me on my environment.

Create a Console Project

We’ll demonstrate how User Secrets works by implementing a simple Console .NET 6 application.

Image 1

The new layout of the Console .NET 6 project you will be presented with just a Program.cs with no plumbing in place – here, we will add the code needed:

Image 2

Add UserSecrets NuGet Package to Project

Add the following NuGet package to your project:

<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" 
                  Version="6.0.1" />

Image 3

This will, in turn, create an entry in your project properties file called UserSecretsId:

Image 4

This GUID entry is used to build up the path to the newly created file named secrets.json in your (windows) environment. You won’t see the file inside the solution explorer as it’s saved within your roaming data. The physical path to your secrets.json file (not included in your code check-in) is:

C:\Users\oneillb\AppData\Roaming\Microsoft\UserSecrets\
   050d3b82-cafe-4826-93ed-e156a0ec77be\

Image 5

Open User Secrets

Right click on your project and select the context menu option Manage User Secrets.

Image 6

This will open your secrets.json file. Initially, your secrets will be empty, as you have yet to create any entries.

Image 7

The trick is to keep the same json structure in both the secrets.json and appsettings.json file

Appsettings.json (Development or Production)

Image 8

Image 9

Image 10

Secrets.json

Image 11

Thus, when you reference the settings, it doesn’t matter which json file is loaded, the composite key will be valid.

Register Appsetting and User Secrets at Start-Up

In the snippet of code below (in your Program.cs file), you are creating what would have been the Configuration method to register your pipeline objects (middleware, dependency injection, for e.g.)

Image 12

The order that you load your settings in is important, as the last setting referenced will be the one that gets used (last in wins – if two appsetting files get loaded and they both have a common connection string, the last loaded, that connection string will get used.)

In the code above, I am building up one of the Appsetting files, based on the runtime environment setting – see how it is set below (to Development):

Image 13

Image 14

NB: If your Appsetting file doesn’t exist – no exception is thrown!

A side note, I am retrieving the Environment variable using the snippet of code below – this is then used to build up the Appsetting file name that I wish to import (I don’t have to import this specific Appsetting – I could load production and override it with my User Secrets).

Image 15

If I just want to referencing User Secrets in my code:

Image 16

Reading User Secret Values - Runtime

Once I have loaded the Appsetting and\or User Secrets, and because I have the same json key\value structure within the file, I can make the reference one and it will load the correct setting. There are a couple of ways that I can load the value based on a key.

Retrieving and setting the return type (using the fully qualified json structure):

C#
string fullyQualifiedConnectionString = 
config.GetValue<string>("ConnectionStrings:MyMusicShopConnection"); // read connection

Retrieving a value using the fully qualified json structure:

C#
var fullyQualifiedConnectionStringShortHand = 
    config["ConnectionStrings:MyMusicShopConnection"];

Retrieving a value using the section qualifier (GetConnectionString):

C#
var conString = config.GetConnectionString("MyMusicShopConnection");

Retrieving a root level value:

C#
var userId = config["UserID"]; // no parent & shorthand

Snippet of code used:

Image 17

Revision History

  • 27th May, 2022: Initial draft (v1)
  • 30th May, 2022: Update - additional topics (v2)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Ireland Ireland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionCouldn't find Microsoft.Extensions.Configuration.UserSecrets Pin
RodAtHome9-Nov-22 2:23
RodAtHome9-Nov-22 2:23 
AnswerRe: Couldn't find Microsoft.Extensions.Configuration.UserSecrets Pin
RodAtHome10-Nov-22 15:20
RodAtHome10-Nov-22 15:20 
GeneralMy vote of 5 Pin
abhi_here8-Nov-22 9:17
abhi_here8-Nov-22 9:17 
QuestionGreat ! Pin
Ondřej Koller7-Nov-22 0:24
Ondřej Koller7-Nov-22 0:24 
QuestionUserSecretsId is not a Guid Pin
rallets6-Nov-22 23:46
rallets6-Nov-22 23:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.