简体   繁体   English

在 .Net Core 3.1 中解密 app.config 连接字符串

[英]Decrypting app.config connection strings in .Net Core 3.1

I have the following code for a console application:我有以下控制台应用程序代码:

        // 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;
        }

This code works just fine on Framework 4.8, but when I try it on Core 3.1, it throws "PlatformNotSupportedException" at the "UNPROTECT" code.这段代码在 Framework 4.8 上工作得很好,但是当我在 Core 3.1 上尝试时,它在“UNPROTECT”代码处抛出“PlatformNotSupportedException”。

This is on the same work station and everything.这是在同一个工作站和一切。

The official documentation for both ConfigurationManager and SectionInformation shows compatibility with Core 3.0 and 3.1. ConfigurationManager 和 SectionInformation 的官方文档都显示了与 Core 3.0 和 3.1 的兼容性。

I'm guessing that the classes are "compatible" with Core for convenience of accessing the config file but the decryption is not because the keys for decryption are stored in the Framework, but Core is cross platform and therefore won't have access to the keys.我猜为了方便访问配置文件,这些类与 Core“兼容”,但解密不是因为解密的密钥存储在框架中,而是 Core 是跨平台的,因此无法访问键。 (Yes?) (是的?)

If this platform doesn't support decryption of connection strings, is there some preferred alternative to encrypting/decrypting connection strings?如果此平台不支持解密连接字符串,是否有一些首选的替代加密/解密连接字符串的方法?

I have looked high and low but don't seem to be able to find anything.我看了高低,但似乎无法找到任何东西。

NOTE: The ability to decrypt the encrypted connection string is essential!注意:解密加密连接字符串的能力是必不可少的!

Thanks.谢谢。

The ConfigurationManager class has been deprecated in DotNetCore, it has been replaced with the IConfiguration class which you can construct with the ConfigurationBuilder class, here's an example of loading a json file (just a note that you need two nuget dependencies for this which are Microsoft.Extensions.Configuration and Microsoft.Extensions.Configuration.Json ): ConfigurationManager类在 DotNetCore 中已被弃用,它已被替换为IConfiguration类,您可以使用ConfigurationBuilder类构造它,这是加载 json 文件的示例(请注意,您需要两个 nuget 依赖项,它们是Microsoft.Extensions.ConfigurationMicrosoft.Extensions.Configuration.Json ):


var config = new ConfigurationBuilder()
    .AddJsonFile("Config.json", true) // bool to say whether it is optional
    .Build()

As mentioned this will give you an instance of the IConfiguration class which is documented here but is very similar to the ConfigurationManager如前所述,这将为您提供IConfiguration类的一个实例,该实例记录在此处,但与ConfigurationManager非常相似

example of config.json : config.json示例:

{
  "ConnectionStrings": {
    "BloggingDatabase": "Server=(localdb)\\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;"
  },
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM