Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open web.config from console application?

Tags:

I have a console capplication that runs on the same computer that hosts a bunch of web.config files. I need the console application to open each web.config file and decrypt the connection string and then test if the connection string works.

The problem I am running into is that OpenExeConfiguration is expecting a winforms application configuration file (app.dll.config) and OpenWebConfiguration needs to be run through IIS. Since this is my local machine, I'm not running IIS (I use Visual Studio's built-in server).

Is there a way I can open the web.config files while still getting the robustness of .NET's capabilities to decrypt the connectionstrings?

Thanks

Update The OpenWebConfiguration works if you are querying IIS directly or are the website in question that you want to look up the web.config for. What I am looking to accomplish is the same sort of functionality, but from a console application opening up the web.config file of a website on my same machine not using an IIS query because IIS isn't running on my machine.

like image 355
Josh Avatar asked Feb 04 '09 21:02

Josh


People also ask

Can we add Web config in console application?

as web. config, but for console and WinForms applications. To add one to your project, right-click the project in Solution Explorer, Add..., New Item... and pick "Application Configuration File" from the Templates box.


2 Answers

Ok I got it... compiled and accessed this so i know it works...

      VirtualDirectoryMapping vdm = new VirtualDirectoryMapping(@"C:\test", true);             WebConfigurationFileMap wcfm = new WebConfigurationFileMap();             wcfm.VirtualDirectories.Add("/", vdm);               // Get the Web application configuration object.             Configuration config = WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");              ProtectSection(config, @"connectionStrings", "DataProtectionConfigurationProvider"); 

This is assuming you have a file called web.config in a directory called C:\Test.

I adjusted @Dillie-O's methods to take a Configuration as a parameter.

You must also reference System.Web and System.configuration and any dlls containing configuration handlers that are set up in your web.config.

like image 96
Jeff Martin Avatar answered Oct 06 '22 02:10

Jeff Martin


The when the ConfigurationManager class grab a section from the config file, it has an "IsProtected" property that it can infer for a given section that you grab. If it is protected, you can then Unprotect it using some code.

The basic method for encrypting/decrypting goes like this (taken from article link below):

private void ProtectSection(string sectionName, string provider) {     Configuration config =         WebConfigurationManager.             OpenWebConfiguration(Request.ApplicationPath);      ConfigurationSection section =                  config.GetSection(sectionName);      if (section != null &&               !section.SectionInformation.IsProtected)     {         section.SectionInformation.ProtectSection(provider);         config.Save();     } }  private void UnProtectSection(string sectionName) {     Configuration config =         WebConfigurationManager.             OpenWebConfiguration(Request.ApplicationPath);      ConfigurationSection section =               config.GetSection(sectionName);      if (section != null &&           section.SectionInformation.IsProtected)     {         section.SectionInformation.UnprotectSection();         config.Save();     } } 

Check out this article for the full details on working with this.

like image 41
Dillie-O Avatar answered Oct 06 '22 01:10

Dillie-O