.NET 4.7连接字符串格式的用户秘密 [英] User Secrets in .NET 4.7 connectionstrings format

查看:48
本文介绍了.NET 4.7连接字符串格式的用户秘密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经花了几个小时研究并不断提供有关.NET Core的信息,但几乎没有有关.NET 4.7完整框架的信息.我想出了如何将User Secrets添加到Web API解决方案的主项目中.我得到基本的secrets.xml文件,在其中我需要存储数据库用户名和密码或连接字符串.我发现的每篇文章都谈到了您需要对web.config进行的更改.但是,在secrets.xml文件中,没有任何内容显示如何处理我的连接字符串,如何设置其格式.我可以创建一个名称/值对,但是似乎什么也没做,我的应用无法连接到数据库.

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.

我的Web.config中有这个文件:

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>

我的secrets.xml看起来像这样:

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?

推荐答案

这是我能够基于 Web.config

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

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

settings.json

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

这篇关于.NET 4.7连接字符串格式的用户秘密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆