如何判断我的connectionStrings部分是否加密?

时间:2017-08-04 21:16:00

标签: vb.net winforms encryption connection-string app-config

我已按照Microsoft的说明here来加密我的应用程序的连接字符串。我选择将连接字符串移动到他们自己的配置文件connections.config。我的代码如下所示(页面底部) - 几乎与Microsoft提供的代码段完全相同。执行操作后,命令:MessageBox.Show(String.Format("Protected={0}", connectionStringsSection.SectionInformation.IsProtected))打印True,表示操作成功。

但是,Microsoft声明“以下配置文件片段在加密后显示connectionStrings部分:

configProtectionProvider="DataProtectionConfigurationProvider">  
  <EncryptedData>  
    <CipherData>  
      <CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAH2... </CipherValue>  
    </CipherData>  
  </EncryptedData>  

在我的情况下,这似乎不正确。尽管在询问我的True时是否打印sectionInformation.IsProtected,但在视觉检查时,我的app.config和我的connections.config文件都保持不变。这引出了我的问题:

  1. 我的问题的描述是否表明事实上我的部分毕竟不受保护?
  2. 为什么sectionInformation.IsProtected打印True,但我的<EncryptedData>没有添加sectionInformation个属性?
  3. 如果您仔细阅读上面链接中的Microsoft说明,它们会提供有关在app.config之外创建自己的connections.config文件以及加密连接部分的说明。但是,他们没有明确表示按照他们的加密connectionStrings部分的说明将在外部配置文件,connections.config以及中这样做。 app.config中的属性标记<connectionStrings configSource="connections.config" />是否足以确保此行为?
  4. 如何测试我的应用程序的连接字符串是否确实已正确加密,而不是MessageBox打印IsProtected属性?
  5. 注意

    1. 这不是ASP.Net应用程序,这是一个Winforms应用程序
    2. 我在他们自己的connections.config文件以及app.config文件中使用我的connectionStrings测试了上面的内容,结果是一样的。
    3. 我的代码的相关部分发布在下面:

      *app.config*
      
      <?xml version="1.0" encoding="utf-8" ?>
      <configuration>
          <configSections>
              <!-- ... -->
          </configSections>
          <system.diagnostics>
              <!-- ... -->
          </system.diagnostics>
          <userSettings>
              <!-- ... -->
          </userSettings>
        <connectionStrings configSource="connections.config" />
      </configuration>
      
      *connections.config*
      
      <connectionStrings>
        <!--Manhattan Connection-->
        <add name="MANHATTAN"
             connectionString="Data Source=xxx;Initial Catalog=xxx;Persist Security Info=False;Integrated Security=False" />
        <add name="DENVER"
           connectionString="Data Source=xxx;Initial Catalog=xxx;Persist Security Info=False;Integrated Security=False" />
        <add name="DESMOINES"
           connectionString="Data Source=xxx;Initial Catalog=xxx;Persist Security Info=False;Integrated Security=False" />
      </connectionStrings>
      
      *The connection source code*
      
      Private Sub ToggleConfigurationEncryption(ByVal executableName As String)
          Try
              Dim configManager = ConfigurationManager.OpenExeConfiguration(executableName)
              Dim connectionStringsSection = configManager.GetSection("connectionStrings")
              If connectionStringsSection.SectionInformation.IsProtected Then
                  connectionStringsSection.SectionInformation.UnprotectSection()
              Else
                  connectionStringsSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider")
              End If
              configManager.Save()
              MessageBox.Show(String.Format("Protected={0}", connectionStringsSection.SectionInformation.IsProtected))
          Catch ex As Exception
              ExceptionController.LogException(ex)
              ExceptionController.DisplayException(ex)
          End Try
      End Sub
      

1 个答案:

答案 0 :(得分:0)

您的问题发生在if声明中。

If connectionStringsSection.SectionInformation.IsProtected Then
        connectionStringsSection.SectionInformation.UnprotectSection()
...

此示例显示如何在加密和解密之间切换。在您发布的代码中,如果文件已经加密,您只需解密该文件即可。你的代码应该是这样的:

If (Not connectionStringsSection.SectionInformation.IsProtected) Then
    connectionStringsSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider")
End If

如果文件尚未加密,您只想加密。不要担心解密文件并尝试阅读。它会自动为您解密。

我没有看到你如何调用ToggleConfigurationEncryption方法。您需要传递输出配置文件的正确名称。启动应用程序后(如果处于调试模式),您可以转到bin\debug文件夹中的项目目录并查找connections.config文件。当您打开它时,您会看到它已加密。