是ConfigurationManage - > section.SectionInformation.ProtectSection()机器依赖?

Fab*_*bio 8 cryptography connection-string app-config

在代码中

Configuration config = ConfigurationManager.OpenExeConfiguration (Application.ExecutablePath);
ConnectionStringsSection section = config.GetSection("connectionStrings") as ConnectionStringsSection;
if (!section.SectionInformation.IsProtected)
{
    section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
}
Run Code Online (Sandbox Code Playgroud)

当我将应用程序移动到另一台机器时,我遇到了一些麻烦.

是section.SectionInformation.ProtectSection调用机器依赖,意思是,我不能复制配置文件并在另一台机器上使用它?

是否有机器独立的提供者(DataProtectionConfigurationProvider除外)?

我的应用程序要求它在具有相同配置文件的多台计算机上运行(它必须从闪存驱动器运行).

谢谢,法比奥

tak*_*krl 10

section.SectionInformation.ProtectSection是否依赖于调用机器,意思是,我无法复制配置文件并在另一台机器上使用它?

是的,就我所知,这是正确的.本文称密钥存储在每台计算机或每用户的基础上.

是否有机器独立的提供者(DataProtectionConfigurationProvider除外)?

不是开箱即用,我所知道的两个提供商(DataProtectionConfigurationProviderRSAProtectedConfigurationProvider)都有相同的"问题".我发现了一些提示,RSA提供程序允许跨机器重用密钥,但没有找到任何关于如何实现这一点的示例.

但是,有一种方法可以实现你所需要的,我昨天自己做了,因为我遇到了类似的问题(我需要从网络位置运行应用程序,并且所有客户端都需要共享相同的加密配置文件) .你可以自己动手ProtectedConfigurationProvider.以下是一些说明这一概念的链接:

使用这些文章,我能够构建自己的ProtectedConfigurationProvider,它不依赖于机器或用户,并在应用程序中使用它.我的发布版本中有一个后构建步骤,可以保护配置部分,因此我只部署它的受保护版本.获取受保护的部分数据可以在其他计算机上正常运行而不会出现任何问题.当然,您必须非常小心如何安全地加密和解密您的部分.这里有几个例子概述了如何做到这一点,我认为是其中之一.

在这三篇文章中没有明确说明的一件事是,如果您不使用ASP.net,如何让您的应用找到您的提供商.将它安装到全局程序集缓存中的常用方法可能不适合您,因为您声明从闪存驱动器运行应用程序.因此,您需要将其添加到您的app.config文件中,类似于:

<?xml version="1.0"?>
<configuration>
  ... 
  <configProtectedData defaultProvider="MyEncryptionProvider">
    <providers>
      <add name="MyEncryptionProvider"
        type="MyAssembly.MyEncryptionProvider, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=whatever_the_assembly_token_is" />
    </providers>
  </configProtectedData>
  ...
</configuration>
Run Code Online (Sandbox Code Playgroud)

如果执行加密的程序集与主程序集位于同一路径中,则此方法应该有效.我正在使用已签名的程序集,sn -T {Assembly}将为您提供需要在配置文件中输入的PublicKeyToken.

然后保护一个部分类似于这样:

using System.Configuration;

...

Configuration oConfiguration = ConfigurationManager.OpenExeConfiguration(yourExePath);
oSection.SectionInformation.ProtectSection("MyEncryptionProvider");
oSection.SectionInformation.ForceSave = true;
oConfiguration.Save();
Run Code Online (Sandbox Code Playgroud)

我今天测试了它,它使用在开发机器(XP SP3)上加密的配置文件,并在XP SP2,Win7 32Bit和Win7 64Bit上使用.

免责声明

  • 如果您不签署程序集,则不确定是否有任何此作用.
  • 使用风险自负,我不是任何标准的安全专家.