How to encrypt app.config?

Create app.config in wpf (C #)

<?xml version="1.0"?> <configuration> <connectionStrings> <clear /> <add name="Name" providerName="MySql.Data" connectionString="Server=.net;Uid=;Pwd=H;Database=;charset=utf8;Allow Zero Datetime=true;" /> </connectionStrings> </configuration> 

C # code used:

  Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConnectionStringsSection conStr = config.ConnectionStrings; if (!conStr.SectionInformation.IsProtected) { conStr.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider"); conStr.SectionInformation.ForceSave = true; config.Save(); } else { foreach (ConnectionStringSettings ss in conStr.ConnectionStrings) Console.WriteLine(ss); Console.Read(); } 

config.Save(); - raises an exception:

{"Failed to encrypt the" connectionStrings "section with the provider 'RsaProtectedConfigurationProvider. Error message from provider: object already exists. \ R \ n"}

+6
security c # wpf connection-string
source share
3 answers
+1
source share

I had the same exception in Save. By running the application as an administrator, I was able to get around this.

I added the app.manifest file to my project and changed the execution level as follows: requestExecutionLevel level = "requireAdministrator" uiAccess = "false"

Thus, I always start as an administrator and have permissions to save the encrypted partition.

0
source share

You can use aspnet_regiis.exe to perform encryption. See This MSDN Link

This way you can perform encryption without writing code.

-one
source share

All Articles