Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
i write a program with c# and it work very good on my laptop when bebug it with visual studio now i transfer it (exe file and access file ) to an other computer it does not work

my connection string :
HTML
<connectionStrings>
  <add name="Citybuilding" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Citybuilding.accdb;Jet OLEDB:Database Password=3051629170;"/>
</connectionStrings>


and decript code :

C#
public class ReturnConnectionString
   {

       public static string ConnectionString
      {
           get
          {
              protection();
              return ConfigurationManager.ConnectionStrings["Citybuilding"].ConnectionString;
          }
      }

       public static void protection()
       {
           ToggleConnectionStringProtection(Application.ExecutablePath, true);
       }

       private static void ToggleConnectionStringProtection(string Path, bool Protect)
       {
           // Define the Dpapi provider name.
           string strProvider = "DataProtectionConfigurationProvider";
           // string strProvider = "RSAProtectedConfigurationProvider";

           System.Configuration.Configuration oConfiguration = null;
           System.Configuration.ConnectionStringsSection oSection = null;

           try
           {
               // Open the <span class="highlight">configuration</span> file and retrieve the connectionStrings section.

               // For Web!
               // oConfiguration = System.Web.Configuration.WebConfigurationManager.O  penWebConfiguration("~");

               // For Windows!
               // Takes the executable file name without the config extension.
               oConfiguration = System.Configuration.ConfigurationManager.OpenExeConfiguration(Path);

               if (oConfiguration != null)
               {
                   bool blnChanged = false;
                   oSection = oConfiguration.GetSection("connectionStrings") as System.Configuration.ConnectionStringsSection;

                   if (oSection != null)
                   {
                       if ((!(oSection.ElementInformation.IsLocked)) && (!(oSection.SectionInformation.IsLocked)))
                       {
                           if (Protect)
                           {
                               if (!(oSection.SectionInformation.IsProtected))
                               {
                                   blnChanged = true;

                                   // Encrypt the section.
                                   oSection.SectionInformation.ProtectSection(strProvider);
                               }

                           }
                           else
                           {
                               if (oSection.SectionInformation.IsProtected)
                               {
                                   blnChanged = true;

                                   // Remove encryption.
                                   oSection.SectionInformation.UnprotectSection();
                               }
                           }
                       }

                       if (blnChanged)
                       {
                           // Indicates whether the associated <span class="highlight">configuration</span> section will be saved even if it has not been modified.
                           oSection.SectionInformation.ForceSave = true;

                           // Save the current <span class="highlight">configuration</span>.
                           oConfiguration.Save();
                       }
                   }
               }
           }
           catch (System.Exception ex)
           {
               throw (ex);
           }
           finally
           {
           }


       }
   }


and error is :
Failed to decrypt using provider 'DataProtectionConfigurationProvider'. Error message from the provider: Key not valid for use in specified state. (Exception from HRESULT: 0x8009000B) (C:\Users\PC-01\Desktop\ShareFolder\New folder (3)\Citybuilding.exe.config line 12)/nSystem.Configuration/n   at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult)
   at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.Configuration.GetSection(String sectionName)
   at Citybuilding.Models.ReturnConnectionString.ToggleConnectionStringProtection(String Path, Boolean Protect) in d:\Project\Citybuilding\Citybuilding\Citybuilding\Models\ReturnConnectionString.cs:line 74
   at Citybuilding.Models.ReturnConnectionString.protection() in d:\Project\Citybuilding\Citybuilding\Citybuilding\Models\ReturnConnectionString.cs:line 25
   at Citybuilding.Models.ReturnConnectionString.get_ConnectionString() in d:\Project\Citybuilding\Citybuilding\Citybuilding\Models\ReturnConnectionString.cs:line 18
   at Citybuilding.Models.ObjCityForm..ctor() in d:\Project\Citybuilding\Citybuilding\Citybuilding\Models\ObjCityForm.cs:line 46
   at Citybuilding.Forms.frmControl.btnFind_Click(Object sender, EventArgs e) in d:\Project\Citybuilding\Citybuilding\Citybuilding\Forms\frmControl.cs:line 78/nSystem.Runtime.InteropServices.COMException (0x8009000B): Key not valid for use in specified state. (Exception from HRESULT: 0x8009000B)
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
   at System.Configuration.DpapiProtectedConfigurationProvider.DecryptText(String encText)
   at System.Configuration.DpapiProtectedConfigurationProvider.Decrypt(XmlNode encryptedNode)
   at System.Configuration.ProtectedConfigurationSection.DecryptSection(String encryptedXml, ProtectedConfigurationProvider provider)
   at System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.DecryptSection(String encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfigSection)
   at System.Configuration.Internal.DelegatingConfigHost.DecryptSection(String encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfigSection)
   at System.Configuration.Internal.DelegatingConfigHost.DecryptSection(String encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfigSection)
   at System.Configuration.BaseConfigurationRecord.CallHostDecryptSection(String encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfig)
   at System.Configuration.BaseConfigurationRecord.DecryptConfigSection(ConfigXmlReader reader, ProtectedConfigurationProvider protectionProvider)
Posted
Comments
[no name] 19-May-15 2:29am    
http://community.discountasp.net/threads/encrypting-webconfig-connection-strings.20562/

did you change the connectionstring ? every computer have a difference Name.

try to check app.config in the program folder and change it there
 
Share this answer
 
You have encrypted the config file on your laptop and it can only be decrypted on same computer.
To make it work you have to encrypt it on target computer

May be u can try a custom installation to do this best of luck
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900