从控制台应用程序打开的web.config? [英] Open web.config from console application?

查看:158
本文介绍了从控制台应用程序打开的web.config?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有承载一束web.config文件中在同一台计算机上运行一个控制台capplication。我需要的控制台应用程序来打开每个web.config文件和解密连接字符串,然后进行测试如果连接字符串的工作。

我遇到的问题是,OpenExeConfiguration期待一个WinForms应用程序配置文件(app.dll.config)和OpenWebConfiguration需要通过IIS运行。由于这是我的本地机器,我没有运行IIS(我使用Visual Studio内置的服务器)。

有没有一种方法,我可以打开web.config文件中同时还获得.NET程序的能力来解密的ConnectionStrings健壮性?

感谢

更新
该OpenWebConfiguration工作,如果你是直接查询IIS还是要查找的web.config中有问题的网站。我所希望实现的是同一类的功能,但是从控制台应用程序打开了一个网站的web.config文件我的同一台机器上不使用IIS查询,因为IIS是不是我的机器上运行。


解决方案

好吧,我知道了...编译和访问这所以我知道它的工作原理...

  VirtualDirectoryMapping VDM =新VirtualDirectoryMapping(@C:\\测试,真正的);
            WebConfigurationFileMap wcfm =新WebConfigurationFileMap();
            wcfm.VirtualDirectories.Add(/,VDM);
            //获取的Web应用程序配置对象。
            配置配置= WebConfigurationManager.OpenMappedWebConfiguration(wcfm,/);            ProtectSection(配置,@是connectionStrings,DataProtectionConfigurationProvider);

这是假设你有一个名为web.config文件中的目录名为C:\\测试

我调整@ Dillie-O的方法,采取配置作为参数。<​​/ P>

您还必须引用System.Web程序和System.configuration以及包含设置在web.config配置处理器任何DLL。

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.

解决方案

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.

这篇关于从控制台应用程序打开的web.config?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆