加密WebConfig

w00*_*977 8 asp.net

我有一个Web应用程序,我使用'publish'选项将其发布到三个Web服务器.

我想加密Web配置文件的connectionstrings部分.以下命令将执行此操作:

c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pef "connectionStrings" c:\inetpub\application
Run Code Online (Sandbox Code Playgroud)

但是,我必须将RDP(远程桌面)连接到每个服务器并在每个服务器上运行命令,因为您无法像这样(从客户端PC)运行它:

\ servername\c $\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pef"connectionStrings"\ servername\c $\inetpub\application

有没有更好的方法:或许:

1)发布后在服务器上执行命令行2)在Visual Studio中使用构建选项,允许您在发布完成后执行批处理文件

Aus*_*Rob 5

在服务器上对web.config的connectionStrings部分进行加密,然后将此加密的部分添加到您的web。[CONFIGURATION_FOR_SERVER] .config转换文件中。密钥是第一行,表示用此新的加密值替换原始web.config的connectionStrings部分。对于要发布到的每个服务器,您将需要一个新的转换文件。Visual Studio将发出警告(不是错误),即

Warning 15  The element 'connectionStrings' has invalid child element 'EncryptedData' in namespace 'http://www.w3.org/2001/04/xmlenc#'. List of possible elements expected: 'add, remove, clear'.   C:\DevTFS\YourProject\Web.Stage.config  14  6   YourProject
Run Code Online (Sandbox Code Playgroud)

关于此转换文件的格式-我没有找到正确的语法来解决这个问题,因此我乐于接受建议,但仍然可以使用,所以我很高兴。有关此内容的完整博客文章:http : //randomdotnetnuggets.blogspot.com.au/2013/05/publishing-encrypted-connection-strings.html

<connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider" xdt:Transform="Replace">
<EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
  xmlns="http://www.w3.org/2001/04/xmlenc#">
  <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
  <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
    <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
      <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
      <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
        <KeyName>Rsa Key</KeyName>
      </KeyInfo>
      <CipherData>          
         <CipherValue>t8p7aOZTjMo...zE6FAAI=</CipherValue>
      </CipherData>
    </EncryptedKey>
  </KeyInfo>
  <CipherData>
    <CipherValue>Vy1TZWY8....ic+Qg6T7U</CipherValue>
  </CipherData>
</EncryptedData>
Run Code Online (Sandbox Code Playgroud)


Ray*_*eng 1

如果集成安全性不是一个选项,我建议您使用MS Web Deploy

当您使用 Visual Studio 2012 构建部署包时,您将获得 zip 文件和命令行脚本文件。您可以修改该脚本文件以加密您的 web.config 或滚动您自己的批处理脚本或 powershell 脚本。