简体   繁体   English

在新环境中从C#WinForms应用程序读取加密的app.config时,ConfigurationErrorsException

[英]ConfigurationErrorsException when reading encrypted app.config from C# WinForms application in new environment

I'm building a WinForms application in C# / VS2010 / .Net4.0. 我正在C#/ VS2010 / .Net4.0中构建WinForms应用程序。 My development machine is running Windows 8.1 64 bit. 我的开发计算机正在运行Windows 8.1 64位。 The application makes extensive use of app.config (user scope) to store settings. 该应用程序大量使用app.config(用户范围)来存储设置。 These include certain product registration details so the whole file has been encrypted using RSAProtectedConfigurationProvider. 其中包括某些产品注册详细信息,因此整个文件已使用RSAProtectedConfigurationProvider进行了加密。 The application has an initial run (launched by the installer) in which initial settings are calculated and written before the whole thing gets encrypted. 该应用程序有一个初始运行(由安装程序启动),在该初始运行中,在对整个事物进行加密之前计算并写入了初始设置。 This works fine on my development machine and the application is subsequently able to read the encrypted settings successfully. 这在我的开发机器上可以正常工作,并且该应用程序随后能够成功读取加密的设置。

A problem arises however when I test the application in other environments (Windows 7 32/64-bit and Vista 32/64-bit under Hyper-V with .NET4.0 Client Profile installed). 但是,当我在其他环境(安装了.NET4.0 Client Profile的Hyper-V下的Windows 7 32/64位和Vista 32/64位)中测试应用程序时,会出现问题。 Here the encryption is working fine but when settings are subsequently accessed a ConfigurationErrorsException is thrown by System.Configuration.BaseConfigurationRecord.GetSectionRecursive. 在这里,加密工作正常,但是随后访问设置时,System.Configuration.BaseConfigurationRecord.GetSectionRecursive引发ConfigurationErrorsException。

I realise that the RSA encryption keys are local to an individual machine / user so that encrypted app.config files can't "travel" to other systems but here the initial encrypting is being done on the same (virtual) machine and in the same user context as is subsequently used to read the settings. 我意识到RSA加密密钥对于单个计算机/用户而言是本地的,因此加密的app.config文件无法“传播”到其他系统,但是在此初始加密是在同一台(虚拟)计算机和同一台计算机上完成的用户上下文(随后用于读取设置)。

I've tried running an unencrypted version in these other test environments and everything works fine but I do need to sort this out with an encrypted version. 我已经尝试在其他测试环境中运行未加密的版本,并且一切正常,但是我确实需要使用加密的版本来解决这个问题。

One thing I notice is that, while my test VMs use the .NET Framework 4 Client Profile (via the web installer), my Windows 8.1 build system has something called the ".NET Framework 4 Multi-Targeting Pack" installed. 我注意到的一件事是,尽管我的测试VM使用.NET Framework 4客户端配置文件(通过Web安装程序),但我的Windows 8.1构建系统已安装了称为“ .NET Framework 4 Multi-Targeting Pack”的东西。 One theory I have therefore is that there is some discrepancy here but you would think that, if the encryption works, then unencryption in the same environment should work as well. 因此,我有一个理论是这里存在一些差异,但是您会认为,如果加密有效,那么在相同环境中进行解密也应该有效。

Any ideas most welcome. 任何想法最欢迎。

I found that if I changed the namespace of the application, this error would pop up (I guess it corrupted the config file). 我发现,如果我更改了应用程序的名称空间,则会弹出此错误(我猜它损坏了配置文件)。 Since the settings are copied to your %AppData% folder, I just changed the version of the application and did a rebuild. 由于设置已复制到您的%AppData%文件夹,因此我只是更改了应用程序的版本并进行了重建。 The error went away. 错误消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM