Freigeben über


SYSK 187: The New Way to Get Configuration Settings

In ASP.NET 2.0, you can use the new class – WebConfigurationManager – to access machine and application information.  Better yet, there are a number of framework classes that allow for strongly-typed access of configuration section settings.  For example, to get the current trust level specified as:

<system.web>

  <trust level="Full" originUrl=""/>

</system.web>

 

you would execute something like this:

System.Web.Configuration.TrustSection section = System.Web.Configuration.WebConfigurationManager.GetWebApplicationSection("system.web/trust") as System.Web.Configuration.TrustSection;

        Response.Write(section.Level);

 

Here is a list of all (?) classes representing configuration sections, in alphabetical order:

· AnonymousIdentificationSection

· AppSettingsSection

· AuthenticationModulesSection

· AuthenticationSection

· AuthorizationSection

· CacheSection

· ClientSettingsSection

· ClientTargetSection

· CompilationSection

· ConnectionManagementSection

· ConnectionStringsSection

· CustomErrorsSection

· DefaultProxySection

· DefaultSection

· DefaultSettingsSection

· DeploymentSection

· DeviceFiltersSection

· GlobalizationSection

· HealthMonitoringSection

· HostingEnvironmentSection

· HttpCookiesSection

· HttpHandlersSection

· HttpModulesSection

· HttpRuntimeSection

· IdentitySection

· IgnoreSection

· ImageGenerationSection

· MachineKeySection

· MachineSettingsSection

· MailSettingsSectionGroup

· MembershipSection

· NetSectionGroup

· OutputCacheSection

· OutputCacheSettingsSection

· PagesSection

· ProcessModelSection

· ProfileSection

· ProtectedConfigurationSection

· RequestCachingSection

· RoleManagerSection

· SecurityPolicySection

· SessionPageStateSection

· SessionStateSection

· SettingsSection

· SiteCountersSection

· SiteMapSection

· SmtpMailSection

· SmtpSection

· SqlCacheDependencySection

· TraceSection

· TransactionsSectionGroup

· TrustSection

· UrlMappingsSection

· WebControlsSection

· WebPartsSection

· WebRequestModulesSection

· WindowsFormsSection

· XhtmlConformanceSection

Comments

  • Anonymous
    August 28, 2006
    Good to know this. Thanks for sharing.
  • Anonymous
    August 28, 2006
    Hi Irena

    Great resource as usual.

    But could you please, oh please, tell us how to encrypt a section/string in the config-file. I can't make this happen correctly, but I've seen some really simple code before.

    Thank you.
  • Anonymous
    August 29, 2006
    Are you working on a windows or web app?  Are you trying to encrypt an application/web config file or machine.config?  Without more specific information, all I can do is provide a generic answer, which follows:

    A command-line utility, aspnet_regiis.exe, allows you to encrypt certain portions of the Web.config, such as the <connectionStrings>, <compilation>, and <authentication> sections.

    You can also programatically encrypt configuration sections by using the System.Configuration.SectionInformation class:

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

  • Anonymous
    August 30, 2006
    Thank you Irina. I meant Web-config, but I thought it was similar to encode the config in both WinApps and WebApps.

    One last question, where do I find out what string to put in the "string provider" in the ProtectSection-method?

    Thank you again
  • Anonymous
    August 31, 2006
    .NET has a couple of provider classes -- DpapiProtectedConfigurationProvider and RsaProtectedConfigurationProvider.  So, you could use:  section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");

    See http://msdn2.microsoft.com/en-us/library/system.configuration.sectioninformation.protectsection.aspx for a complete example.
  • Anonymous
    October 13, 2007
    The comment has been removed