2013-04-20 122 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

相關問題