asp.net - 加密 Web 配置

标签 asp.net

我有一个 Web 应用程序,我使用“发布”选项将其发布到三个 Web 服务器。

我想加密 Web 配置文件的连接字符串部分。下面的命令可以做到这一点:

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

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

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

是否有更好的方法来做到这一点:也许:

1) Execute a command line on the server after publishing 2) Use a build option in Visual Studio that allows you to execute a batch file after publishing is complete

最佳答案

加密服务器上 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

关于此转换文件的格式 - 我还没有找到解决此问题的正确语法,因此我愿意接受建议,但它仍然有效,所以我很高兴。完整的博客条目: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>

关于asp.net - 加密 Web 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14838156/

相关文章:

asp.net - 在呈现的 HTML 中为自定义 UserControl 提供一个 ID

asp.net - TypeScript - [后续属性声明必须具有相同类型] - 对相同类型定义的多个引用

asp.net - IHttpActionResult - 如何使用?

jquery - 如何使用JQuery在下拉列表中选择默认值

c# - 从 IFrame 重定向父页面

c# - 更新字段时违反了 UNIQUE KEY 约束

javascript - 如何使用代码为我的div中的文本添加颜色

c# - RestClient 应该是单例还是为每个请求创建新实例

c# - 将列表从代码隐藏转移到 aspx 页面

c# - 无法在 GridView ASP.NET 中设置列​​宽(GridView 没有数据源)