0

我在 Web.config 中完成了以下操作

    <configuration>
  <configSections> 
    <section name="secureAppSettings" type="System.Configuration.NameValueSectionHandler, System, Version=4.0.0.0, Culture=neutral  PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" allowLocation="true" />
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>
  ... etc

然后在 Views/Web.config 我尝试像这样使用它

    <secureAppSettings>
    <!--Captcha keys-->
    <add key="RecaptchaPrivateKey" value="somevalue" />
    <add key="RecaptchaPublicKey" value="someothervalue" />

  </secureAppSettings>

但是每次尝试打开该站点时,我都会收到以下错误:

配置错误无法读取配置部分“secureAppSettings”,因为它缺少部分声明

我不明白我应该做什么。我目前正在尝试在这样的视图中使用它:

@((NameValueCollection)WebConfigurationManager.GetSection("secureAppSettings"))["RecaptchaPublicKey"]

编辑:

我已经尝试按照其中一条评论建议将 configSections 移至 View/web.config

结果是以下错误消息

解析器错误消息:为 secureAppSettings 配置部分创建处理程序时发生错误:指定的程序集名称或指定的代码库无效。(来自 HRESULT 的异常:0x80131047)

我认为这是我做错了的标志,但我现在看到,也许我需要在这里修复一些东西,而不是按照我正在做的其他方式去做。

但是对我来说,这个信息和以前一样神秘。

澄清我要归档的内容。

我的 Web.config 中有一些我想加密的敏感信息。我读到我可以创建一个secureAppSettings 部分,然后从那里对其进行加密。我想将信息从 appSettings 部分中分离出来的原因是因为我有很多其他信息,我真的不需要加密。最终,所有这些都将导致我能够制作一个 MSI 网站安装程序,在安装过程中,我可以在加密 secureAppSettings 之前更改或添加 appSettings 和 secureAppSettings 部分的设置。以便于设置和安装我正在构建的站点。

现在我不确定我的解决方案目前的方式是否可行,但如果不是,那么我想指出正确的方向,以解决我的最终目标,即能够制作 MSI 安装程序,我可以在安装过程中更改配置文件并加密任何敏感信息。

4

1 回答 1

0

在回复我的评论时,您提到您还尝试使用根web.config文件而不是Views文件夹中的文件进行设置。

理论上它必须可以使用该Views\web.config文件。我只成功使用了
root 。 如下所示。ASP.NET MVCweb.config

请注意,我使用了一个不太明确的section定义。

网页配置

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

    <!-- Other configuration elements -->

    <secureAppSettings>
        <!--Captcha keys-->
        <add key="RecaptchaPrivateKey" value="somevalue" />
        <add key="RecaptchaPublicKey" value="someothervalue" />
    </secureAppSettings>
</configuration>

在我看来

 <span>@(((NameValueCollection)WebConfigurationManager.GetSection("secureAppSettings"))["RecaptchaPublicKey"])</span>
于 2018-08-03T09:01:22.997 回答