.NET 4.7连接字符串格式的用户秘密

编程入门 行业动态 更新时间:2024-10-28 04:21:11
本文介绍了.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?

推荐答案

这是我能够基于 github/aspnet/MicrosoftConfigurationBuilders

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连接字符串格式的用户秘密

本文发布于:2023-10-28 13:09:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1536724.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   秘密   格式   用户   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!