{"id":11301,"date":"2025-02-10T22:28:27","date_gmt":"2025-02-10T22:28:27","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=11301"},"modified":"2025-02-10T22:28:27","modified_gmt":"2025-02-10T22:28:27","slug":"using-secrets-in-your-appsettings-json-via-visual-studio-2022-and-dotnet-cli","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/using-secrets-in-your-appsettings-json-via-visual-studio-2022-and-dotnet-cli\/","title":{"rendered":"Using secrets in your appsettings.json via Visual Studio 2022 and dotnet CLI"},"content":{"rendered":"<p>You&#8217;ve got yourself an appsettings.json file for your ASP.NET core application and you&#8217;re using sensitive data, such as passwords or other secrets. Now you obviously don&#8217;t want to commit those secrets to source control, so you&#8217;re not going to want to store these values in your appsettings.json file.<\/p>\n<p>There&#8217;s several ways to achieve this, one of those is to use Visual Studio 2022 &#8220;Manage User Secrets&#8221; option which is on the context menu off of your project file. There&#8217;s also the ability to use to dotnet CLI for this as we&#8217;ll see later.<\/p>\n<p>This context menu option will create a secrets.json in %APPDATA%\\Microsoft\\UserSecrets\\{Guid}. The GUID is stored within your .csproj in a PropertyGroup like this<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;UserSecretsId&gt;0e6abf63-deda-47fc-9a80-1cb56abaeead&lt;\/UserSecretsId&gt;\r\n<\/pre>\n<p>So the secrets file can be used like this<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n{\r\n  &quot;ConnectionStrings:DefaultConnection&quot;: &quot;my-secret&quot;\r\n}\r\n<\/pre>\n<p>and this will map to your appsettings.json, that might look like this<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n{\r\n  &quot;ConnectionStrings&quot;: {\r\n    &quot;DefaultConnection&quot;: &quot;not set&quot;\r\n  },\r\n}\r\n<\/pre>\n<p>Now we can access the configuration in the usual way, for example <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nbuilder.Configuration.AddUserSecrets&lt;Program&gt;();\r\n\r\nvar app = builder.Build();\r\nvar connectionString = app.Configuration.GetSection(&quot;ConnectionStrings:DefaultConnection&quot;);\r\nvar defaultConnection = connectionString.Value;\r\n<\/pre>\n<p>When somebody else clones your repository you&#8217;ll need to recreate the secrets file, we could use _dotnet user-secrets_ for example<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ndotnet user-secrets set &quot;ConnectionStrings:DefaultConnection&quot; &quot;YourConnectionString&quot;\r\n<\/pre>\n<p>and you can list the secrets using<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ndotnet user-secrets list\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You&#8217;ve got yourself an appsettings.json file for your ASP.NET core application and you&#8217;re using sensitive data, such as passwords or other secrets. Now you obviously don&#8217;t want to commit those secrets to source control, so you&#8217;re not going to want to store these values in your appsettings.json file. There&#8217;s several ways to achieve this, one [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[200],"tags":[],"class_list":["post-11301","post","type-post","status-publish","format-standard","hentry","category-asp-net-core"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11301","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=11301"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11301\/revisions"}],"predecessor-version":[{"id":11306,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11301\/revisions\/11306"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=11301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=11301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=11301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}