aspnet / MicrosoftConfigurationBuilders

Microsoft.Configuration.Builders
MIT License
122 stars 61 forks source link

Accessing user secrets from my program? #237

Closed jjrobert65 closed 5 months ago

jjrobert65 commented 5 months ago

This is almost certainly a misunderstanding or typo on my part but I haven't been able to figure out how to access the user secret I've set up from my code.

I have a .NET Framework 4.8 Web API project.

I right-clicked into "Manage User Secrets" in Visual Studio and let it install Microsoft.Configuration.ConfigurationBuilders.UserSecrets and update my Web.config:

Here are the important bits of my Web.config:

  <configSections>
    <section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />
  </configSections>
  <configBuilders>
    <builders>
      <add name="Secrets" userSecretsId="583724f7-d984-4688-84cd-cb8b28df5527" mode="Greedy" type="Microsoft.Configuration.ConfigurationBuilders.UserSecretsConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.UserSecrets, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </builders>
  </configBuilders>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

and the secrets.xml file exists and has one secret:

$ cat ~/AppData/Roaming/Microsoft/UserSecrets/583724f7-d984-4688-84cd-cb8b28df5527/secrets.xml
<?xml version="1.0" encoding="utf-8"?>
<root>
  <secrets ver="1.0" >
    <secret name="FTP_SERVER" value="acme.com" />
  </secrets>
</root>

In one of my controllers I've tried to do this:

                string username = ConfigurationManager.AppSettings["FTP_SERVER"];

but I always get null.

I thought maybe it was an issue with the key name or something but when I loop over ConfigurationManager.AppSettings.Keys as in your examples and dump what's there, I only get the four values in the portion of Web.config:

webpages:Version    3.0.0.0
webpages:Enabled    false
ClientValidationEnabled true
UnobtrusiveJavaScriptEnabled    true

Am I wrong to expect to read these from Configuration.AppSettings ? Am I missing part of the glue?

Thanks, -Jeff

StephenMolloy commented 5 months ago

A config builder can inject information into any configuration section that it knows about. (Out of the box, that's AppSettings and ConnectionStrings... but you can create section handlers to work with other config sections if needed.) So just declaring a configuration builder doesn't automatically get you where you want because the builder doesn't know where to put it's settings.

Long story short - you need to put a configBuilders="Secrets" attribute on your <appSettings> section to tell the 'Secrets' config builder to apply it's values to the appSettings section. Then you should see your secret values show up in ConfigurationManager.AppSettings.

jjrobert65 commented 5 months ago

Thanks so much! Now I have to go re-read all the articles and see how many times I missed that!

-Jeff