【发布时间】: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 文件在目视检查时都保持不变。这引出了我的问题:
- 我的问题描述是否表明事实上我的部分根本没有受到保护?
- 为什么
sectionInformation.IsProtected打印True,但我的sectionInformation中没有添加<EncryptedData>属性? - 如果您仔细阅读上面链接中的 Microsoft 说明,它们会提供有关在 app.config 之外创建您自己的 connections.config 文件以及加密您的连接部分的说明。但是,他们并没有明确说明按照他们的说明加密 connectionStrings 部分将在外部配置文件中执行,connections.config 也是如此。 app.config 中的属性标签
<connectionStrings configSource="connections.config" />是否足以确保这种行为? - 除了打印
IsProtected属性的MessageBox 之外,我如何测试我的应用程序的连接字符串是否确实正确加密?
注意
- 这不是 ASP.Net 应用程序,这是 Winforms 应用程序
- 我已经在他们自己的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