Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User Secrets in .NET 4.7 connectionstrings format

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?

like image 266
Connie DeCinko Avatar asked Dec 30 '19 20:12

Connie DeCinko


1 Answers

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",
}
like image 73
Gregory Mertens Avatar answered Nov 15 '22 23:11

Gregory Mertens