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

3

我遇到了一个之前遇到过的问题,但找不到解决方法的参考。

这里是问题所在。我们使用以下代码加密客户端应用程序中的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

2
如果您使用的是Vista或更高版本,请确保以管理员身份运行。 - Jeremy McGee
4个回答

2
以下是我对使用受保护的配置文件加密配置文件的所有步骤进行加密并将其部署到Web服务器的清单,希望能够帮助到某些人...:http://blogs.msdn.com/mosharaf/archive/2005/11/17/protectedConfiguration.aspx#1657603。请复制粘贴 :D。日期为 2007 年 2 月 12 日星期一凌晨 12:15,作者是 Naica。
  1. To create a machine-level RSA key container

    aspnet_regiis -pc "DataProtectionConfigurationProviderKeys" -exp
    
  2. Add this to web.config before connectionStrings section:

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

    Do not miss the <clear /> from above! Important when playing with encrypting/decrypting many times

  3. Check to have this at the top of Web.Config file. If missing add it:

    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
    
  4. Save and close Web.Config file in VS (very important!)

  5. In Command Prompt (my local PC) window go to:

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

  6. Encrypt: (Be aware to Change physical path for your App, or use -app option and give the name o virtual directory for app! Because I used VS on my PC I preferred the bellow option. The path is the path to Web.config file)

    aspnet_regiis -pef "connectionStrings" "c:\Bla\Bla\Bla" -prov "DataProtectionConfigurationProvider"

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

  7. To Decrypt (if needed only!):

    aspnet_regiis -pdf "connectionStrings" "c:\Bla\Bla\Bla"
    
    aspnet_regiis -pdf "system.web/membership" "c:\Bla\Bla\Bla"
    
  8. Delete Keys Container (if needed only!)

    aspnet_regiis -pz "DataProtectionConfigurationProviderKeys"
    
  9. Save the above key to xml file in order to export it from your local PC to the WebServer (UAT or Production)

    aspnet_regiis -px "DataProtectionConfigurationProviderKeys" \temp\mykeyfile.xml -pri
    
  10. Import the key container on WebServer servers:

    aspnet_regiis -pi "DataProtectionConfigurationProviderKeys" \temp\mykeyfile.xml
    
  11. Grant access to the key on the web server

    aspnet_regiis -pa "DataProtectionConfigurationProviderKeys" "DOMAIN\User"
    

    See in IIS the ASP.NET user or use:

    Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name
    
  12. Remove Grant access to the key on the web server (Only if required!)

    aspnet_regiis -pr "DataProtectionConfigurationProviderKeys" "Domain\User"
    
  13. Copy and Paste to WebServer the encrypted Web.config file.


1

我找到了一种比我最初给自己的答案更优雅的解决方案。我发现,如果我只是以最初安装应用程序并导致配置文件连接字符串被加密的用户身份登录,并在命令提示符中转到 .net 框架目录并运行

aspnet_regiis -pa "NetFrameworkConfigurationKey" "{domain}\{user}"

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

我只是想在这里添加一下,因为我认为我已经在我们的开发博客上记录了这个问题,但是在这里找到了它,所以以防我需要再次查找,它将在这里。还会在我们的开发博客中添加指向此线程的链接。


这也有助于在从ASP .NET应用程序中获取错误时进行处理。 aspnet_Regiis -pa“NetFrameworkConfigurationKey ASPNET” - Jay

1

所以我成功了。

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

但这只是为这个用户使它工作了。

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

任何建议都可以帮助我指向正确的方向,因为我对这种RSA加密类型的东西不太熟悉。


0
听起来像是权限问题。所涉及到的(新)用户是否具有对app.config文件的写入权限?之前的用户是否是本地管理员或能够掩盖这个问题的电源用户?

我们已经使用ClickOnce安装为新用户安装了应用程序。两个用户都在PC的管理员组中。从我在网上找到的信息来看,似乎默认的RSA密钥是机器特定的,当不同的用户使用它来保护他们的app.config版本时会导致错误。我认为我需要使用一些用户特定的密钥进行保护,现在只需要找出如何做。:)谢谢回复!Mike - MikeScott8

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接