Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to decrypt using provider 'RsaProtectedConfigurationProvider'

In my application, that connecting to MS Sql database, I am using Microsoft.Data.ConnectionUI And my application work in my computer. If i run this application in another computer, when i open connection dialog, i see that error: enter image description here

That 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 I need to do with this?

like image 965
Alexander Mashin Avatar asked Apr 24 '13 08:04

Alexander Mashin


2 Answers

Bad Data is usually caused by using the wrong key. It sounds like you are encrypting the .config file on one machine (your dev machine?) and the attempting to decrypt on a different machine. This will not work as the decryption key is missing.

The encrypted config section should be encrypted on the machine where the application is running so that it uses the appropriate key.

like image 112
Remus Rusanu Avatar answered Oct 18 '22 22:10

Remus Rusanu


Yes, what comes to my mind is that you have enabled encryption in your Web.Config to the <connectionStrings> section. This type of problem occurs when you deploy your application on a machine on which encryption was not performed. Encryption uses Machine level key which would be missing on your development machine. Following options come to my mind

  1. You can either remove encryption on the original machine & run the application
  2. Alternatively you can export the RSA key container used on the original machine during encryption & import that specific key container on your new machine
like image 44
Zo Has Avatar answered Oct 18 '22 22:10

Zo Has