C# 解密.Net Core 3.1中的app.config连接字符串
我有以下控制台应用程序代码:C# 解密.Net Core 3.1中的app.config连接字符串,c#,frameworks,connection-string,app-config,core,C#,Frameworks,Connection String,App Config,Core,我有以下控制台应用程序代码: // Get the app config file. var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // Get the sections to unprotect. ConfigurationSection connStrings = configuration
// Get the app config file.
var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Get the sections to unprotect.
ConfigurationSection connStrings = configuration.ConnectionStrings;
string connection = null;
if (connStrings != null)
{
// UNPROTECT
connStrings.SectionInformation.UnprotectSection();
connection = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
}
这段代码在Framework4.8上运行得很好,但是当我在Core3.1上尝试它时,它会在“UNPROTECT”代码处抛出“PlatformNotSupportedException”
这是在同一个工作站和一切
ConfigurationManager和SectionInformation的官方文档显示了与Core 3.0和3.1的兼容性
我猜这些类与Core“兼容”是为了方便访问配置文件,但解密并不是因为解密密钥存储在框架中,而是Core是跨平台的,因此无法访问密钥。(是吗?)
如果此平台不支持对连接字符串进行解密,是否存在加密/解密连接字符串的首选替代方案
我到处寻找,但似乎什么也找不到
注意:解密加密连接字符串的能力至关重要
谢谢。在DotNetCore中,
ConfigurationManager
类已被弃用,它已被可使用ConfigurationBuilder
类构建的IConfiguration
类取代,下面是加载json文件的示例(请注意,您需要两个nuget依赖项,它们是Microsoft.Extensions.Configuration
和Microsoft.Extensions.Configuration.Json
):
如前所述,这将为您提供一个IConfiguration
类的实例,该类已记录在案,但与ConfigurationManager
config.json
示例:
var config = new ConfigurationBuilder()
.AddJsonFile("Config.json", true) // bool to say whether it is optional
.Build()
{
"ConnectionStrings": {
"BloggingDatabase": "Server=(localdb)\\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;"
},
}
它会处理这些值的加密/解密吗?我在那个类中没有看到任何表明这一点的东西。@aaron看一下这个