You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何正确加密web.config?求助使用-site参数指定路径的示例

解决aspnet_regiis加密connectionStrings时的配置文件错误

当然可以用-site参数来指定web.config的准确位置,这个参数能帮你精准定位目标网站的配置文件,解决之前出现的"A configuration file cannot be created for the requested Configuration object."错误——大概率是单独用-app参数时,路径没有被系统正确识别导致的。

操作步骤与示例:

  1. 先获取目标网站的ID
    你可以通过IIS管理器查看网站列表里的"ID"列,或者用命令行快速查询:

    appcmd list site
    

    输出会类似:SITE "MyWebsite" (id:2,bindings:http/*:80:,state:Started),这里的2就是对应网站的ID。

  2. 带-site参数的加密命令示例
    假设你的网站ID是2,目标应用的路径是/MyApp,完整的加密命令如下:

    aspnet_regiis -pe "connectionStrings" -site 2 -app "/MyApp" -prov "RsaProtectedConfigurationProvider"
    

    如果要加密根网站的web.config(没有子应用),可以省略-app参数:

    aspnet_regiis -pe "connectionStrings" -site 1 -prov "RsaProtectedConfigurationProvider"
    

额外注意事项:

  • 务必以管理员身份运行命令提示符,否则可能没有权限修改web.config文件。
  • 你的当前web.config片段里缺少connectionStrings节点,需要先在<configuration>根节点下添加该节才能进行加密,示例如下:
    <configuration>
      <connectionStrings>
        <add name="YourDatabaseConn" connectionString="Data Source=XXX;Initial Catalog=XXX;User ID=XXX;Password=XXX" providerName="System.Data.SqlClient" />
      </connectionStrings>
      <!-- 原有的system.web等节点 -->
      <system.web>
        <!-- 原内容 -->
      </system.web>
    </configuration>
    
  • 确认RsaProtectedConfigurationProvider已经在web.config的configProtectedData节中正确配置(默认情况下IIS会自带基础配置,若没有则需要手动添加)。

你提供的当前web.config片段:

<!-- Web.Config Configuration File --> 
<configuration> 
  <system.web> 
    <identity impersonate="true" password="somepassword" userName="someusername" /> 
    <customErrors mode="Off" /> 
    <compilation defaultLanguage="c#" debug="true"> 
      <assemblies> 
        <add assembly="Microsoft.VisualBasic, Version=10.0.0.0, Culture=neutral, PublicKeyToken=sometoken" /> 
      </assemblies> 
    </compilation> 
  </system.web> 
</configuration>

内容的提问来源于stack exchange,提问作者Mark McGown

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:07:05