【问题标题】:How can i tell if my connectionStrings section is encrypted or not?如何判断我的 connectionStrings 部分是否已加密?
【发布时间】:2018-01-12 21:54:30
【问题描述】:

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

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

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

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

  1. 我的问题描述是否表明事实上我的部分根本没有受到保护?
  2. 为什么sectionInformation.IsProtected 打印True,但我的sectionInformation 中没有添加&lt;EncryptedData&gt; 属性?
  3. 如果您仔细阅读上面链接中的 Microsoft 说明,它们会提供有关在 app.config 之外创建您自己的 connections.config 文件以及加密您的连接部分的说明。但是,他们并没有明确说明按照他们的说明加密 connectionStrings 部分将在外部配置文件中执行,connections.config 也是如此。 app.config 中的属性标签&lt;connectionStrings configSource="connections.config" /&gt; 是否足以确保这种行为?
  4. 除了打印IsProtected 属性的MessageBox 之外,我如何测试我的应用程序的连接字符串是否确实正确加密?

注意

  1. 这不是 ASP.Net 应用程序,这是 Winforms 应用程序
  2. 我已经在他们自己的connections.config文件以及app.config文件中使用我的connectionStrings测试了上述内容,结果是一样的。

我的代码的相关部分贴在下面:

*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

【问题讨论】:

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


    【解决方案1】:

    您的问题出现在您的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 文件。当你打开它时,你会看到它是加密的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多