Demystifying User Secrets in .Net Core

User Secrets are a convenient way to store local configuration values in your .Net Core applications.

When you create a new .Net Core application, a usersecrets.json file will be automatically created for you to store your local config values. You can manage the values inside your solution by opening up the json file through the web project context menu.





Inside this file, you will see an empty object by default. You can copy over your entire appsettings.json or specific sections and modify them as you see fit for local use. When you run the solution, the config values in usersecrets.json will merge with appsettings.json and overwrite any values that match.



If you want to see where the file is located, right-click the open tab in VS and click "Open Containing Folder". This will take you to your local AppData > Roaming directory.






If you back out of this directory, you will see folders for all of your apps and you will notice that MS uses guids to make each one unique.



GOTCHA - If you are copying the same solution, you will likely end up with 2 solutions pointing to the same usersecrets.json file. Making a change in one solution will reflect the change in the other, which is less than ideal. 

To fix this issue, make sure you have 2 unique folders with a usersecrets.json file inside them. Then you can tell each solution which one to use by editing the Project File. If you open up the .csproj file in the solution, at the top you will see a node for the UserSecretsId. It is the folder name that contains the usersecrets.json file. Edit it to point to your unique folder name.




Comments

Popular posts from this blog

ASP.NET Identity Remember Me

IIS Express Client Certificates

ASP.NET MVC - How to enable/disable CaC/Client Certificate authentication per area or route.