Click here to Skip to main content
15,891,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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'.

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()


Please help me through . Thanks in advance.
Posted
Updated 1-Sep-15 16:56pm
v2

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