从控制台应用程序打开web.config?

问题描述 投票:23回答:4

我有一个控制台capplication,它运行在托管一堆web.config文件的同一台计算机上。我需要控制台应用程序打开每个web.config文件并解密连接字符串,然后测试连接字符串是否有效。

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

有没有办法可以打开web.config文件,同时还能获得.NET解密连接字符串的强大功能?

谢谢

更新如果您直接查询IIS或者您想要查找web.config的网站,OpenWebConfiguration可以正常工作。我想要完成的是同样的功能,但是从控制台应用程序打开同一台机器上的网站的web.config文件而不是使用IIS查询,因为IIS没有在我的机器上运行。

asp.net web-config
4个回答
38
投票

好的我得到了...编译并访问了这个我知道它有效...

      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");

假设您在名为C:\ Test的目录中有一个名为web.config的文件。

我调整了@ Dillie-O的方法,将Configuration作为参数。

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


3
投票

当ConfigurationManager类从配置文件中获取一个部分时,它具有一个“IsProtected”属性,它可以推断出您抓取的给定部分。如果它受到保护,您可以使用某些代码取消保护它。

加密/解密的基本方法是这样的(取自下面的文章链接):

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();
    }
}

查看this article了解有关使用此功能的完整详细信息。


0
投票

我想你想用它的OpenWebConfiguration方法使用WebConfigurationManager类。

它需要一个指向web.config的路径,并且应该像在基于HTTPContext的应用程序中那样打开它。


0
投票
    public static string WebKey(string key)
    {

        var configFile = new System.IO.FileInfo(webconfigPath);
        var vdm = new VirtualDirectoryMapping(configFile.DirectoryName, true, configFile.Name);
        var wcfm = new WebConfigurationFileMap();
        wcfm.VirtualDirectories.Add("/", vdm);
        System.Configuration.Configuration config = WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
        System.Configuration.AppSettingsSection appSettingSection = (System.Configuration.AppSettingsSection)config.GetSection("appSettings");
        System.Configuration.KeyValueConfigurationElement kv = appSettingSection.Settings.AllKeys
                         .Where(x => x.Equals(key))
                         .Select(x => appSettingSection.Settings[key])
                         .FirstOrDefault();

        return kv != null ? kv.Value : string.Empty;

    }
© www.soinside.com 2019 - 2024. All rights reserved.