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

问题描述 投票:3回答:4

我遇到了以前的问题;找不到我如何解决它的参考。

这是问题所在。我们使用以下代码为客户端应用程序加密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
vb.net cryptography app-config cryptographicexception
4个回答
2
投票

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

复制粘贴:D

2007年2月12日,星期一,上午12:15,Naica

re:使用受保护的配置加密配置文件

以下列出了我在PC上加密两个部分然后将其部署到WebServer的所有步骤。也许它会帮助某人......:

  1. 创建机器级RSA密钥容器 aspnet_regiis -pc "DataProtectionConfigurationProviderKeys" -exp
  2. 在connectionStrings部分之前将其添加到web.config: <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. 在命令提示符(我的本地PC)窗口中转到: C:\ WINNT \ Microsoft.NET \框架\ V2.0.50727
  6. 加密:(注意更改应用程序的物理路径,或使用-app选项并为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. 删除密钥容器(仅在需要时!) aspnet_regiis -pz "DataProtectionConfigurationProviderKeys"
  9. 将以上密钥保存到xml文件,以便将其从本地PC导出到WebServer(UAT或Production) aspnet_regiis -px "DataProtectionConfigurationProviderKeys" \temp\mykeyfile.xml -pri
  10. 在Web Server服务器上导入密钥容器: qazxsw poi
  11. 授予对Web服务器上密钥的访问权限 aspnet_regiis -pi "DataProtectionConfigurationProviderKeys" \temp\mykeyfile.xml 在IIS中查看ASP.NET用户或使用: aspnet_regiis -pa "DataProtectionConfigurationProviderKeys" "DOMAIN\User"
  12. 删除对Web服务器上密钥的授予访问权限(仅在需要时!) Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name
  13. 将加密的Web.config文件复制并粘贴到WebServer。

1
投票

我找到了一个更优雅的解决方案,在我对自己的原始回答中。我发现如果我只是以root用户身份登录,并且安装了应用程序并导致配置文件连接字符串被加密并在commadn提示符下转到.net framework目录并运行

aspnet_regiis -pr "DataProtectionConfigurationProviderKeys" "Domain\User"

它授予其他用户访问RSA加密密钥容器的权限,然后它适用于其他用户。

只是想在这里添加它,因为我认为我在我们的开发博客上发布了这个问题,但是在这里找到它,所以如果我需要再次查找它将会在这里。将在此主题中添加指向我们的开发博客点的链接。


1
投票

所以我确实让它运转了。

  1. 从笔记本电脑中删除旧用户帐户
  2. 重置app.config以使部分不受保护
  3. 从所有用户机器密钥中删除了密钥文件
  4. 运行app并允许它保护该部分

但所有这一切都让它为这个用户工作。

现在我需要知道我要做些什么来更改代码以保护该部分,以便PC上的多个用户可以使用该应用程序。我来的是虚拟PC(明天下周三到WDW度假之后)!

任何有助于指导我正确方向的建议,因为我对这种RSA加密类型的东西不是很有经验。


0
投票

听起来像权限问题。有问题的(新)用户对app.config文件有写权限吗?以前的用户是否可能掩盖了此问题的本地管理员或超级用户?

© www.soinside.com 2019 - 2024. All rights reserved.