我正在开发一个带有 Visual Studio 2013 (.NET 4.5)Oracle Developer Tools 最新版本的MDI应用程序 . 我需要创建一个主窗体,然后它将成为许多不同窗体的容器,每个窗体都是不同的应用程序 .

应用程序必须连接到不同的Oracle数据库,现在我正在尝试设置主窗体以保持所有连接字符串的加密,并可能在单独的文件上 . 现在连接字符串只有一个 .

要加密连接字符串,我使用了这些文章中描述的 aspnet_regiis 方法:

总结一下:

我在app.config中添加了 <configProtectedData> 部分,我在其中定义了自己的RSA提供程序和密钥容器,因为我需要在多台计算机上导出和导入密钥 . 然后我创建了密钥容器并将其与 aspnet_regiis 工具一起使用来加密 <connectionStrings> 部分 .

我还为 TNS_ADMIN 变量设置 <oracle.manageddataaccess.client> 部分以使用我自己的 Oracle Instant Client 和我的自定义 tnsnames.ora 文件 . 这将有助于避免不同的机器操作系统和Oracle客户端版本和配置出现问题 .

那么,现在我的最终 app.config 文件应该是这样的:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
  </configSections>
  <oracle.manageddataaccess.client>
    <version number="*">
      <settings>
        <setting name="TNS_ADMIN" value="D:\VSProjects\Visual Studio 2013\Projects\MDITest\MDITest\"/>
      </settings>
    </version>
  </oracle.manageddataaccess.client>
  <configProtectedData>
    <providers>
      <add name="OracleDeveloperRSAProtectedConfigurationProvider"
    type="System.Configuration.RsaProtectedConfigurationProvider,System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    keyContainerName="OracleDeveloperDbKeys"
    description="Uses RsaCryptoServiceProvider to encrypt and decrypt" />
    </providers>
  </configProtectedData>
  <connectionStrings configProtectionProvider="OracleDeveloperRSAProtectedConfigurationProvider">
    <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>ZuB8jsXnXR/6Ww47R4Oc/ksSgHsrRuuOkNypbsdfm1ASDlvdsklsgfhtrwaADFHrywswvfhgnjlsGHSDJKFEROvfsd/TV+LKlysPkccEXmJFCcFZ7S9geSInPBaNvYGweR9FcTK1HVcrYMaddgfBK6lpSTTw6cdMRIOcw0Ib//oYPr34=</CipherValue>
          </CipherData>
        </EncryptedKey>
      </KeyInfo>
      <CipherData>
        <CipherValue>01old8NrGlRAOLdfdtXUKYuBkZPY5XbWMI/j22Hnm8U=</CipherValue>
      </CipherData>
    </EncryptedData>
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

现在我需要使用数据源配置向导来设置Oracle数据集,然后能够将表从DataSource资源管理器拖放到自动创建导航按钮并保持表之间关系等的表单 .

Data source configuration Wizard

但是当我使用原始 app.config 的新应用程序完成向导配置时,它要求我创建一个ConnectionString并将其存储在 app.config 中,然后副本也放在 Settings.settings 文件中, obviously in plain text .

如果我执行上述过程来加密原始 app.config 中的 <connectionStrings> 部分,那么 DataSet.xsd works only if the ConnectionString setting is present inside the Settings.settings . 它不使用 app.config 参数 .

我试图打开 DataSet.Designer.cs 文件但是为每个 TableAdapter 对象指定了 ConnectionConnectionString 参数,因为're many tables in my dataset, I'd想要避免查找/替换方法...另外因为如果我需要对数据集进行一些更改,那么 DataSet.xsd 文件可能会被覆盖,因此所有编辑都将丢失 .

I want to know which is the best way to replace the connection string and use the encrypted one instead.

最后,我还想知道是否可以在保存加密的同时将 <connectionStrings> 部分移动到另一个.config文件中 .

-- EDIT --

我已经做了一些测试来重新创建一个新的应用程序项目,Visual Studio看起来比我想象的要聪明!

在使用向导设置新数据源后,我将第一个连接字符串添加到空白 app.config . 然后我编辑了 app.config 并将 <connectionStrings> 部分移动到新的 connections.config 文件 . 我应用 aspnet_regiis 方法加密该部分,一切正常 . 但是,因为我仍然可以在 Settings.settings 中的纯文本中看到连接字符串,所以我决定更改 <CipherData> 字符串以查看应用程序是否会抛出错误或者它是否可以正常工作 . 很简单,如果应用程序抛出错误,那么它使用加密的连接字符串(我想要的 - 正确),否则它使用 Settings.settings 上的纯文本连接字符串(我不想要的 - 不正确) . 令人惊讶的是,该应用程序抛出异常!

似乎 Settings.settings 面板总是显示纯文本连接字符串,因为它读取app.config(以及连接.config),然后应用"on the fly"解密 . So that panel can be considered just like a simple view of the app.config that shows some parameters, even if they are encrypted.

除此之外,我再次使用向导添加了另一个带有另一个连接的数据源 . 新连接自动添加到 connections.config 加密!我注意到了,因为 <CipherData> 字符串被更改了 . 此外,新连接在 Settings.settings 内的纯文本中可见 . 因此我通过更改字符串重新执行与之前相同的测试,并且应用程序再次抛出错误 .

所以,最后, there's no need to re-encrypt the app.config file each time you add a new connection because it is automatically done! 太棒了!