vb.net - App.config 连接字符串保护错误

标签 vb.net cryptography app-config cryptographicexception

我遇到了以前遇到的问题;找不到我关于如何解决该问题的引用。

问题就在这里。我们使用以下代码对客户端应用程序的 app.config 中的连接字符串部分进行加密:

        config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
        If config.ConnectionStrings.SectionInformation.IsProtected = False Then
            config.ConnectionStrings.SectionInformation.ProtectSection(Nothing)

            ' We must save the changes to the configuration file.'
            config.Save(ConfigurationSaveMode.Modified, True)
        End If

问题是我们有一名销售人员休假。旧笔记本电脑将交给新销售人员,并在新用户登录下,当它尝试执行此操作时,我们会收到错误。错误是:

Unhandled Exception: System.Configuration.ConfigurationErrorsException: 
An error occurred executing the configuration section handler for connectionStrings. ---> System.Configuration.ConfigurationErrorsException: Failed to encrypt the section 'connectionStrings' using provider 'RsaProtectedConfigurationProvider'. 
Error message from the provider: Object already exists.
---> System.Security.Cryptography.CryptographicException: Object already exists

最佳答案

http://blogs.msdn.com/mosharaf/archive/2005/11/17/protectedConfiguration.aspx#1657603

复制并粘贴:D

2007 年 2 月 12 日星期一 12:15 由 Naica 发布

回复:使用 protected 配置加密配置文件

以下是我在 PC 上加密两个部分然后将其部署到 Web 服务器所执行的所有步骤的列表。也许它会对某人有所帮助...:

  1. 创建计算机级 RSA key 容器

    aspnet_regiis -pc "DataProtectionConfigurationProviderKeys" -exp
    
  2. 将其添加到 web.config 中的 connectionStrings 部分之前:

     <add name="DataProtectionConfigurationProvider"
    
          type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0,
    
                   Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a,
    
                   processorArchitecture=MSIL"
    
          keyContainerName="DataProtectionConfigurationProviderKeys"
    
          useMachineContainer="true" />
    

    不要错过<clear />从上面!多次加密/解密时很重要

  3. 检查是否将其置于 Web.Config 文件的顶部。如果缺少,请添加:

    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
    
  4. 在VS中保存并关闭Web.Config文件(非常重要!)

  5. 在命令提示符(我的本地电脑)窗口中转到:

    C:\WINNT\Microsoft.NET\Framework\v2.0.50727

  6. 加密:(请注意更改应用程序的物理路径,或使用 -app 选项并为应用程序提供虚拟目录的名称!因为我在 PC 上使用 VS,所以我更喜欢以下选项。路径是Web.config 文件的路径)

    aspnet_regiis -pef“connectionStrings”“c:\Bla\Bla\Bla”-prov“DataProtectionConfigurationProvider”

    aspnet_regiis -pef“system.web/membership”“c:\Bla\Bla\Bla”-prov“DataProtectionConfigurationProvider”

  7. 解密(仅在需要时!):

    aspnet_regiis -pdf "connectionStrings" "c:\Bla\Bla\Bla"
    
    aspnet_regiis -pdf "system.web/membership" "c:\Bla\Bla\Bla"
    
  8. 删除 key 容器(仅在需要时!)

    aspnet_regiis -pz "DataProtectionConfigurationProviderKeys"
    
  9. 将上述 key 保存到 xml 文件,以便将其从本地 PC 导出到 Web 服务器(UAT 或 Production)

    aspnet_regiis -px "DataProtectionConfigurationProviderKeys" \temp\mykeyfile.xml -pri
    
  10. 在 WebServer 服务器上导入 key 容器:

    aspnet_regiis -pi "DataProtectionConfigurationProviderKeys" \temp\mykeyfile.xml
    
  11. 授予对网络服务器上 key 的访问权限

    aspnet_regiis -pa "DataProtectionConfigurationProviderKeys" "DOMAIN\User"
    

    在 IIS 中查看 ASP.NET 用户或使用:

    Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name
    
  12. 删除授予对网络服务器上 key 的访问权限(仅在需要时!)

    aspnet_regiis -pr "DataProtectionConfigurationProviderKeys" "Domain\User"
    
  13. 将加密的 Web.config 文件复制并粘贴到 WebServer。

关于vb.net - App.config 连接字符串保护错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42115/

相关文章:

c# - 保存 .NET 用户设置需要很长时间

wpf - 当鼠标指针离开 UIElement 时如何确定鼠标按钮是否被按下

vb.net - VB NET 中最快的排列代码排列数字

Python密码学包RSA——将私钥保存到DB

.net - 通过 ShellExecute 调用应用程序时,未读取应用程序配置文件

.net - WCF 配置 - 将其从 app.config 中分离出来

.net - 使用 VB.NET 从 KML 中提取多边形名称和坐标

mysql - datagridview 到带有内连接的 Crystal 报表

c# - 使用 key 加密和解密的简单方法

cryptography - 将数据拆分为更小的部分,然后使用 RSA 加密