User Secrets in .NET 4.7 connectionstrings format

拜拜、爱过 提交于 2021-02-11 13:55:48

问题


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?


回答1:


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",
}



回答2:


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.



来源:https://stackoverflow.com/questions/59536717/user-secrets-in-net-4-7-connectionstrings-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!