2013-04-20 123 views
5

在我的Windows应用程序,我试图加密app.config文件的连接字符串的部分,我的app.config文件的连接字符串的部分是使用提供者'RsaProtectedConfigurationProvider'解密失败?

<connectionStrings> 
<add name="SQLiteDB" connectionString="Data Source=|DataDirectory|database.s3db;  
Version=3;password=mypassword;" providerName="System.Data.Sqlite"/> 
</connectionStrings> 

和.cs文件我正在对它进行加密像

Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath); 
ConfigurationSection section = config.GetSection("connectionStrings") as ConnectionStringsSection; // could be any section 

if (!section.IsReadOnly()) 
{ 
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider"); 
section.SectionInformation.ForceSave = true; 
config.Save(ConfigurationSaveMode.Full); 
} 

运行此代码后,我得到加密的连接字符串在不同的app.config中,此app.config驻留在bin \ debug文件夹中,此.config文件的名称是nameofapplication.exe.config。

问题是,当我做了这个应用程序的设置,如果提示错误,其他计算机上运行:

System.Configuration.ConfigurationErrorsException: Failed to decrypt using provider 'RsaProtectedConfigurationProvider'. Error message from the provider: The RSA key container could not be opened. 

我这样做第一次所以不知道如何解决这个问题,stucked在严重它。

回答

3

app.config文件将在本地机器上使用证书进行加密。该证书不会出现在另一台机器上。因此您将无法解密app.config文件。

为此,您需要将加密密钥导出到您的机器上,然后将其导入到另一台机器上。下面的文章演示了如何做到这一点:演练:Creating and Exporting an RSA Key Container

+0

仅供参考,或可能是约什导入的钥匙到其他机器上,在我的案件有关许可。当我在VS上进行调试时无法解密,但在以管理员身份打开VS之后。开始解密它。 – enterbutton 2018-02-07 23:12:56

6

使用此命令ASPNET_REGIIS -pa

打开CMD控制台-execute作为管理员 -

C:\Windows\system32>aspnet_regiis -pa "NetFrameworkConfigurationKey" "myDomain\myUser" 
Microsoft (R) ASP.NET RegIIS versión 4.0.30319.33440 
Utilidad de administración que instala y desinstala ASP.NET en el equipo local. 
Copyright (C) Microsoft Corporation. Todos los derechos reservados. 
Agregando ACL para el acceso al contenedor de claves RSA... 
Con éxito 

更多的参考资料:

Ɖiamond ǤeezeƦ answer

The RsaProtectedConfigurationProvider sometimes fails when encrypting an application configuration file

ASP.NET Encryption - aspnet_regiis - Farm

Encrypting and Decrypting Web.config Sections in .NET 4.0

+0

Kiquenet你救了我的一天!将我的笔记本电脑从Win7升级到Win10后,我不断收到此错误。我所要做的就是运行'aspnet_regiis'描述。现在它全部作为一个魅力再次起作用! – 2017-02-19 20:02:31

相关问题