Connection string encryption: "This operation does not apply at run time"

I have a console application and it has app.config. When I run this code:

class Program { static void Main() { ConnectionStringsSection connSection = ConfigurationManager.GetSection("connectionStrings") as ConnectionStringsSection; if (connSection != null) { if (!connSection.SectionInformation.IsProtected) connSection.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider"); else connSection.SectionInformation.UnprotectSection(); } Console.Read(); } } 

I get an error: "This operation does not apply at runtime." I also tried to grant permissions for my app.config, but no luck.

What is the problem?

+4
source share
3 answers

You can try the following:

  static void Main() { // Get the current configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); ConnectionStringsSection connSection = config.GetSection("connectionStrings") as ConnectionStringsSection; if (connSection != null) { if (!connSection.SectionInformation.IsProtected) connSection.SectionInformation.ProtectSection(null); else connSection.SectionInformation.UnprotectSection(); } connSection.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); Console.ReadKey(); } 
+5
source

I think you should use the OpenExeConfiguration method in this scenario:

  System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(pathToExecutable); ConnectionStringsSection connSection = config .GetSection("connectionStrings") as ConnectionStringsSection; 

pathToExecutable should be the full path to the exe of your application, for example: "C: \ application \ bin \ myapp.exe"

+4
source

You do not have to encrypt partitions at runtime; you encrypt them before starting using the aspnet_setreg.exe tool. More info here

ASP.NET then transparently scans the encrypted partitions at runtime.

+2
source

All Articles