Unable to decrypt using provider "RsaProtectedConfigurationProvider"

In my application that connects to the MS Sql database, I use Microsoft.Data.ConnectionUI And my application runs on my computer. If I run this application on another computer when I open the connection dialog, I see this error: enter image description here

This is my code for this:

  try { connectionString = ShowDialogConnection(); SqlConnection connect = new SqlConnection(connectionString); connect.Open(); backgroundWorker1.RunWorkerAsync(); } catch (Exception exc) { MessageBox.Show(exc.ToString()); } string ShowDialogConnection() { string conn = ""; DataConnectionDialog dlg = new DataConnectionDialog(); DataSource.AddStandardDataSources(dlg); dlg.SelectedDataSource = DataSource.SqlDataSource; dlg.SelectedDataProvider = DataProvider.SqlDataProvider; if (ConfigurationManager.ConnectionStrings["ConStr"] != null) { dlg.ConnectionString = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString; } if (DataConnectionDialog.Show(dlg) == DialogResult.OK) { if (dlg.ConnectionString != null && dlg.ConnectionString != "") { conn = dlg.ConnectionString; Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConnectionStringsSection csSection = config.ConnectionStrings; csSection.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider"); csSection.SectionInformation.ForceSave = true; ConnectionStringSettings csSettings = new ConnectionStringSettings("ConStr", dlg.ConnectionString, "System.Data.SqlClient"); if (csSection.ConnectionStrings["ConStr"] != null) csSection.ConnectionStrings.Remove("ConStr"); csSection.ConnectionStrings.Add(csSettings); config.Save(ConfigurationSaveMode.Modified); } } return conn; } 

What do i need to do with this?

+6
source share
2 answers

Bad data is usually caused by the wrong key. It looks like you are encrypting the .config file on one computer (your dev machine?) And trying to decrypt on another machine. This will not work because the decryption key is missing.

The encrypted configuration section must be encrypted on the computer on which the application is running so that it uses the appropriate key.

+10
source

Yes, it seems to me that you have included encryption in your Web.Config in the <connectionStrings> section. This type of problem occurs when you deploy the application to a computer on which encryption has not been performed. Encryption uses a machine level key that will not be available on your development machine. The following options come to my mind

  • You can either remove the encryption on the source computer or run the application
  • Alternatively, you can export the RSA key container used on the source computer during encryption and import this specific key container on your new computer.
+2
source

Source: https://habr.com/ru/post/943516/


All Articles