使用 aspnet_regiis 加密应用程序或 Web.config - 未找到第 'xyz' 部分

Encrypt App or Web.config using aspnet_regiis - Section 'xyz' not found

我的 app.web.config 有自定义配置部分 NameValueSectionHandler,但 aspnet_regiis 找不到它。

我需要将我的 WPF 应用程序部署到多台计算机并对其 app.config 进行加密。我已经用 aspnet_regiis 尝试了许多演练,但没有任何效果。我试过了:

  1. 将 app.config 重命名为 app.web.config
  2. 创建 public 密钥容器 aspnet_regiis -pc LiteContainer -exp
  3. 我一直在加密自定义配置部分 aspnet_regiis -pef connectionSettings D:\Tes -prov LiteProvider

错误是

"The configuration section 'connectionSettings' was not found".
Failed!

但我成功地read/write通过代码将数据添加到此部分。

App/Web.config

<configuration>
    <configSections>
      <section name="connectionSettings" type="System.Configuration.NameValueSectionHandler"/>

      <sectionGroup name="userSettings" .... </sectionGroup>
    </configSections>

    <connectionSettings>
      <server>192.168.1.xxx</server>
      <database>myDb</database>
      <uid>root</uid>
      <pwd>123</pwd>
    </connectionSettings>

  <configProtectedData>
    <providers>
      <add name="LiteProvider"
           keyContainerName="LiteContainer"
           useMachineContainer="true"
           description="Uses RsaCryptoServiceProvider to encrypt and decrypt"
           type="System.Configuration.RsaProtectedConfigurationProvider/>
    </providers>
  </configProtectedData>
</configuration>

我之前没有看到任何演练加密 NameValueSectionHandler,很多使用 applicationSettingsconnectionStrings。我在这里错过了什么?

我认为你的命令是错误的,即使文件夹 D:\Tes 包含你的 web.config:

aspnet_regiis -pef connectionSettings D:\Tes -prov LiteProvider

您输错了 connectionSettings 而不是 connectionStrings:

%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pef "connectionStrings" <full path to directory containing web.config file>

isn't the syntax aspnet_regiis -pef [section name] [web.config path] ? the section name is connectionSettings not connectionStrings


这是我在 PC 上尝试时的结果。

  1. 将带有 AppSettings( 或 ConnectionStrings)的 App.Config 部分复制到 C:\Temp,并将其重命名为 Web.config。

  2. 运行 这个命令: %windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pef "appSettings" c:\Temp

在 运行 aspnet_regiis 命令之后,appSettings 被加密:

  1. 将 C:\Temp\Web.Config 重命名为 App.Config

解决方案

您的 XML 不是预期的格式,例如:

<server>192.168.1.xxx</server>
  <database>myDb</database>
  <uid>root</uid>

使用标准的 appSettings 或 connectionStrings 格式:

<appSettings>
    <add key="server" value="192.168.1.xxx"/>
    <add key="database" value="myDb"/>
    <add key="uid" value="root"/>
    <add key="pwd" value="123"/>
</appSettings>

REF:https://social.msdn.microsoft.com/Forums/windows/en-US/3b5a1d1f-aa57-40d8-8607-fee0b2a8a6db/protect-appconfig-file-or-encrypt?forum=winforms

https://docs.microsoft.com/en-us/dotnet/api/system.configuration.configurationmanager.appsettings?view=netframework-4.7.2

https://docs.microsoft.com/en-us/dotnet/api/system.configuration.configurationmanager.connectionstrings?view=netframework-4.7.2