调试时如何加密连接字符串部分

时间:2018-02-21 01:40:36

标签: azure web-config connection-string aspnet-regiis.exe

我需要以下设置:

  • 在调试我的应用程序时,我希望我的connectionStrings配置部分通过aspnet_regiis加密
  • 发布应用程序时,连接字符串应该是正常的,未加密的<add>元素,其中值只包含一些占位符文本。

我的理由是:

  1. 将代码推送到远程仓库时,我不希望我的调试凭据以明文保存(因此是加密部分)
  2. 将应用程序发布到Azure时,我可以覆盖占位符,来自azure portal的未加密连接字符串
  3. 这是否可以使用配置转换?

    我能找到的唯一示例转换单个连接字符串元素,但我需要整个部分看起来不同,具体取决于调试/发布设置。

    欣赏任何建议

1 个答案:

答案 0 :(得分:0)

我设法在this useful post

的帮助下完成了这项工作
<connectionStrings xdt:Transform="RemoveAttributes(configProtectionProvider)">
    <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
        xmlns="http://www.w3.org/2001/04/xmlenc#" xdt:Transform="Remove" xdt:Locator="Match(Type)" />

    <add xdt:Transform="Insert" name="CDSsvcUsername" connectionString="username"/>
    <add xdt:Transform="Insert" name="CDSsvcPassword" connectionString="password"/>
</connectionStrings>

所以我在整个xdt:Transform="Remove"元素上调用了Encrypted,并删除了configProtectionProvider上的connectionStrings属性。

这仅在删除转换中指定xdt:Locator后才有效。