locked
ConfigurationErrorsException was unhandled while encrypting the connection string of the Web.config file RRS feed

  • Question

  • I tried to encrypt the connection string section of the web.config file via a simple winforms application. I ran the VS2010 as an administrator and ran the following section of code.

         private void encryptButton_Click(object sender, EventArgs e)
                {
        Configuration config = GetConfiguration();
        ConfigurationSection configSection = config.GetSection("connectionStrings");
                    if (configSection != null)
        // Only encrypt the section if it is not already protected
                        if (!configSection.SectionInformation.IsProtected)
        {
                            configSection.SectionInformation.ProtectSection(
        "DataProtectionConfigurationProvider");
                            configSection.SectionInformation.ForceSave = true;
        config.Save();
        DisplayWebConfig();
        }
        }

        private Configuration GetConfiguration() 
        {
                    var configFile = new FileInfo(@"C:\Users\abalawan\Desktop\CN\R13 new\Websites\ABC");
        var vdm = new VirtualDirectoryMapping(configFile.DirectoryName,true,configFile.Name);
        var wcfm = new WebConfigurationFileMap();
        wcfm.VirtualDirectories.Add("/",vdm);
        return WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
        }

    I tried to open the file from a physical path location and write back the encrypted info into it. But config.Save() threw me the below error.

        ConfigurationErrorsException was unhandled.

        Unable to save config to file 'C:\Users\abalawan\Desktop\CN\R13 new\Websites\ABC'.

    I am in urgent need of the solution. Thanks in advance.

    Stacktrace looks like below :

           at System.Configuration.Internal.WriteFileContext.ReplaceFile(String Source, String Target)
           at System.Configuration.Internal.WriteFileContext.Complete(String filename, Boolean success)
           at System.Configuration.Internal.InternalConfigHost.StaticWriteCompleted(String streamName, Boolean success, Object writeContext, Boolean assertPermissions)
           at System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext, Boolean assertPermissions)
           at System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext)
           at System.Configuration.Internal.DelegatingConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext)
           at System.Configuration.UpdateConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext)
           at System.Configuration.MgmtConfigurationRecord.SaveAs(String filename, ConfigurationSaveMode saveMode, Boolean forceUpdateAll)
           at System.Configuration.Configuration.SaveAsImpl(String filename, ConfigurationSaveMode saveMode, Boolean forceSaveAll)
           at System.Configuration.Configuration.Save()
           at EncryptionandDecryptionApplication.Form1.encryptButton_Click(Object sender, EventArgs e) in C:\Users\abalawan\Desktop\CN\EncryptionandDecryptionApplication\EncryptionandDecryptionApplication\Form1.cs:line 55
           at System.Windows.Forms.Control.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ButtonBase.WndProc(Message& m)
           at System.Windows.Forms.Button.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(Form mainForm)
           at EncryptionandDecryptionApplication.Program.Main() in C:\Users\abalawan\Desktop\CN\EncryptionandDecryptionApplication\EncryptionandDecryptionApplication\Program.cs:line 18
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
    • Edited by Recmach Tuesday, September 1, 2015 2:42 PM
    Tuesday, September 1, 2015 2:40 PM

Answers

  • Hi Recmach,

    Please check this case: Error while writing configuration file if the solution works for you.

    General diagnostic is a security problem. 

    Best reagrds,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    Wednesday, September 2, 2015 6:24 AM

All replies

  • Hi Recmach,

    Please check this case: Error while writing configuration file if the solution works for you.

    General diagnostic is a security problem. 

    Best reagrds,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    Wednesday, September 2, 2015 6:24 AM
  • Hi Recmach,

    Since we haven't heard from you for a long time, I assume the issue is resolved, I mark my reply as answer. If you have any concerns, please feel free to reopen it or submit a new question. Thanks for your understanding.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, September 15, 2015 9:19 AM