如何在.NET Core中加密App.config文件?

5
我创建了一个新的.NET Core 2.1控制台项目。 我添加了Config.xml:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
      <add name="MyLocalSQLServer" connectionString="Initial Catalog=aspnetdb;data source=localhost;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>

在 Program.cs 文件中:
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(ConfigurationManager.ConnectionStrings["MyLocalSQLServer"]);            
    }
}

我用 aspnet_regiis 工具对配置文件进行了加密:

RENAME "C:\repos\ConsoleApp7\bin\Debug\netcoreapp2.1\ConsoleApp.dll.config" web.config
aspnet_regiis -pef "connectionStrings" "C:\repos\ConsoleApp\bin\Debug\netcoreapp2.1"
RENAME "C:\repos\ConsoleApp7\bin\Debug\netcoreapp2.1\web.config" ConsoleApp.dll.config

最后,我运行了这个应用:

dotnet "C:\repos\ConsoleApp7\bin\Debug\netcoreapp2.1\ConsoleApp.dll"

我遇到了错误:
Unhandled Exception: System.Configuration.ConfigurationErrorsException: 
 Failed to decrypt using provider 'RsaProtectedConfigurationProvider'. 
 Error message from the provider: Operation is not supported on this platform. 
 ---> System.PlatformNotSupportedException: Operation is not supported on this platform.
   at System.Configuration.RsaProtectedConfigurationProvider.Decrypt(XmlNode encryptedNode)
   at System.Configuration.ProtectedConfigurationSection.DecryptSection(String encryptedXml, ProtectedConfigurationProvider provider)
   at System.Configuration.BaseConfigurationRecord.DecryptConfigSection(ConfigXmlReader reader, ProtectedConfigurationProvider protectionProvider)
   --- End of inner exception stack trace ---
   at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult)
   at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at System.Configuration.ConfigurationManager.get_ConnectionStrings()
   at ConsoleApp7.Program.Main(String[] args)

这个技巧适用于旧的.NET Framework加密app.config,但是它不受.NET Core支持。

其目的是测试从.NET Framework到.NET Core的迁移。

如何在.NET Core中加密app.config?


我无法更正标题。如果有人能够帮忙,那将是愉快的事情。 - vernou
我已经在标题中删除了填充内容,它正在等待同行评审。 - Lucca Ferri
谢谢。这主要是针对aps.net核心API Rest,但也有一些批处理控制台。 - vernou
我知道它的使用方法,只是不明白为什么需要它。它并没有提供太多保护措施,特别是如果有人转储正在运行的进程的内存,而且它会在后期修改时带来问题。但如果你真的需要这样做,我会创建另一个文件,或者在app.config中创建一个加密字符串的部分。 - Lucca Ferri
看起来这个API在.NET Core中不受支持 https://github.com/dotnet/corefx/issues/14950 https://github.com/dotnet/corefx/blob/master/src/System.Configuration.ConfigurationManager/src/System/Configuration/RsaProtectedConfigurationProvider.cs#L19 - TylerReid
显示剩余5条评论
2个回答

4
如“Off The Gold”所述,RsaProtectedConfigurationProvider不受.NET Core和.NET 5+支持。
但是,我在.NET 7.0中使用DataProtectionConfigurationProvider(DPAPI)使其正常工作。
以下是一个有效的encrypt.bat文件:
set mypath=%cd%
rename "app.config" "web.config"

cd "C:\Windows\Microsoft.NET\Framework64\v4.0.30319"
aspnet_regiis.exe -pef "appSettings" "%mypath%" -prov "DataProtectionConfigurationProvider"
aspnet_regiis.exe -pef "connectionStrings" "%mypath%" -prov "DataProtectionConfigurationProvider"

cd "%mypath%" 
rename "web.config" "app.config" 

pause

如果您的项目已经有了一个web.config文件,该怎么办? - Off The Gold
然后删除两行“重命名”代码。 - Tom McElhinney

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接