Open web.config from console application?

后端 未结 4 1823
萌比男神i
萌比男神i 2020-12-13 05:09

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

相关标签:
4条回答
  • 2020-12-13 05:34

    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.

    0 讨论(0)
  • 2020-12-13 05:38

    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.

    0 讨论(0)
  • 2020-12-13 05:43

    I think you want to use WebConfigurationManager class with its OpenWebConfiguration method.

    It takes a path to the web.config and should open it just like it would in a HTTPContext based application.

    0 讨论(0)
  • 2020-12-13 05:54
        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;
    
        }
    
    0 讨论(0)
提交回复
热议问题