Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
565 views
in Technique[技术] by (71.8m points)

asp.net - Open web.config from console application?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

      VirtualDirectoryMapping vdm = new VirtualDirectoryMapping(@"C:est", 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...