【问题标题】:Encrypt App or Web.config using aspnet_regiis - Section 'xyz' not found使用 aspnet_regiis 加密 App 或 Web.config - 未找到“xyz”部分
【发布时间】:2026-01-24 09:40:01
【问题描述】:

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

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

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

错误是

“未找到配置部分'connectionSettings'”。
失败!

但我可以通过代码成功地读取/写入该部分的数据。

应用程序/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。我在这里错过了什么?

【问题讨论】:

  • 等等,你为什么在 WPF 的 app.config 上使用 aspnet_regiis?好的,我没有意识到你可以做到这一点,你是否遵循这些确切的步骤? social.msdn.microsoft.com/Forums/windows/en-US/…
  • 因为许多人说这是可能的,暂时将其重命名为 web.config 并稍后重命名为 .config 是可以的。另外 aspnet_regiis 仅适用于 web.config
  • 我已经尝试了这些步骤
  • D:\Tes 是包含您的 web.config 的文件夹吗?
  • 是的,它位于D:\Tes

标签: c# .net wpf encryption config


【解决方案1】:

我认为你的命令是错误的,即使文件夹 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>

语法不是 aspnet_regiis -pef [section name] [web.config path] 吗?部分名称是 connectionSettings 而不是 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>

参考号: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

【讨论】:

  • 语法不是 aspnet_regiis -pef [section name] [web.config path] 吗?部分名称是 connectionSettings 而不是 connectionStrings
  • @Suwandy +1 让我检查一个 cmd 窗口
  • @Suwandy 看到我的编辑,更改 XML 格式。 同时接受答案可以获得 2 点声望点,一旦你有 15 个代表,你就可以获得投票和标记特权
  • 我仍在弄清楚您的 appSettings 部分来自哪里。您是如何声明该部分的?
  • @Suwandy 我刚写的。在 VS2017 中有一个对话框/窗口可以用来添加 AppSettings 或 ConnectionStrings。我只是对 AppSettings 或 ConnectionStrings 使用了与配置文件期望的键/值 XML 对相同的格式。因此,只需手动复制 my 版本的 your appSettings 并在 app/web 配置中手动输入。