User Secrets in .NET 4.7 connectionstrings format

13.8k Views Asked by At

I have been digging for hours and keep coming up with information about .NET Core, yet hardly anything about .NET 4.7 full framework. I figured out how to add User Secrets to the main project of my Web API solution. I get the basic secrets.xml file where I need to to either store my database username and password or my connection string. Every post I find talks about the changes you need to make to web.config. However nothing shows what to do with my connection string, how to format it, in the secrets.xml file. I could create a name/value pair but that does not seem to do anything, my app cannot connect to the database.

I have this in my Web.config:

  <configBuilders>
    <builders>
    <add name="Secrets" userSecretsId="5c65f7eb-a7e1-46cc-bff4-a526678005f2" type="Microsoft.Configuration.ConfigurationBuilders.UserSecretsConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.UserSecrets, Version=1.0.0.0, Culture=neutral" /></builders>
  </configBuilders>

  <connectionStrings configBuilders="Secrets">
    <add name="ShopAPDbConnectionString" connectionString="" providerName="System.Data.SqlClient" />
  </connectionStrings>

My secrets.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <secrets ver="1.0">
    <secret name="ShopAPDbConnectionString" value="Server=SQLDEV01;Database=ShopAP; Integrated Security=True;" />
  </secrets>
</root>

How do I properly format and get this to work?

4

There are 4 best solutions below

2
On

Here is what I was able to get to work based on https://github.com/aspnet/MicrosoftConfigurationBuilders

Web.config

<configuration>
    <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" userSecretsFile="~/../../../SecretsTest/secrets.xml" mode="Greedy" type="Microsoft.Configuration.ConfigurationBuilders.UserSecretsConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.UserSecrets, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
            <add name="Json" jsonFile="${JSONConfigFileB}" optional="true" type="Microsoft.Configuration.ConfigurationBuilders.SimpleJsonConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Json, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        </builders>
    </configBuilders>
    <!--...-->
    <appSettings configBuilders="Secrets">
        <!--...-->
    </appSettings>
    <!--...-->
    <connectionStrings configBuilders="Json">
        <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="dummy.value.required" />
    </connectionStrings>
    <!--...-->
</configuration>

secrets.xml

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <secrets ver="1.0">
    <secret name="usersecret1" value="dogodog" />
    <secret name="usersecret2" value="secretbar" />
    <secret name="JSONConfigFileB" value="C://Users//xxx//Documents//xxx//xxx//SecretsTest//settings.json" />
  </secrets>
</root>

settings.json

{
  "DefaultConnection": "Server=666.66.666.6;Database=BigToe;User ID=FireBall;Password=BunniesAreSoft",
}
4
On

I am attempting to do the same thing, but unfortunately I think this is only for appSettings when it comes to .Net Framework/Web.config.

0
On

Make sure the only file in your UserSecrets folder (C:\Users...\UserSecrets{guid}) is the secrets.xml file. I was messing around and had a secrets.json file in there (along with the secrets.xml file) and it wouldn't load my secrets.xml file. After I removed the json file, it worked fine.

0
On

Both your files look fine - pretty much exactly the same as mine. How did you create your secrets file? Did you use right-click on the Web project and Manage User Secrets? That will create a file in a folder location in %APPDATA% that should be picked up when you hit F5 in Visual Studio (Debug > Start Debugging). However, it will not be seen by IIS if you compile and then browse to localhost. Even if you change the App Pool Identity in inetmgr to run under your account, and set Load User Profile - it still will not find it.

To find the correct location for the secrets for this case, run your web app and obtain the result of

string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

and copy the secrets file into the matching sub-folder Microsoft\UserSecrets\(guid)\

e.g. C:\Windows\System32\config\systemprofile\AppData\Roaming\Microsoft\UserSecrets\5c65f7eb-a7e1-46cc-bff4-a526678005f2\secrets.xml

you may need to one-off obtain access in Explorer, and create the UserSecrets folder and below.

Remember to update both copies if you edit it in future.