Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

ProtectedConfigurationSection Class

Note: This class is new in the .NET Framework version 2.0.

Provides programmatic access to the configProtectedData configuration section. This class cannot be inherited.

Namespace: System.Configuration
Assembly: System.Configuration (in system.configuration.dll)

public sealed class ProtectedConfigurationSection : ConfigurationSection
public final class ProtectedConfigurationSection extends ConfigurationSection
public final class ProtectedConfigurationSection extends ConfigurationSection

The configProtectedData configuration file section contains a collection of protected data providers in its providers element.

NoteNote

You can use the Aspnet_regiis.exe tool to encrypt and decrypt configuration sections. See Encrypting Configuration Information Using Protected Configuration.

The following configuration file excerpt shows how to declaratively specify protected data providers.

<configProtectedData defaultProvider="RsaProtectedConfigurationProvider">
  <providers>
    <clear />
      <add keyContainerName="NetFrameworkConfigurationKey" cspProviderName="" useMachineContainer="true" useOAEP="false" description="Uses RsaCryptoServiceProvider to encrypt and decrypt" name="RsaProtectedConfigurationProvider" type="System.Configuration.RsaProtectedConfigurationProvider,System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

      <add useMachineProtection="true" description="Uses CryptProtectData and CryptUnProtectData Windows APIs to encrypt and decrypt" keyEntropy="" name="DataProtectionConfigurationProvider" type="System.Configuration.DpapiProtectedConfigurationProvider,System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

  </providers>
</configProtectedData>

The following code example shows how to use the ProtectedConfigurationSection class to programmatically access values in the configProtectedData configuration file section.

using System;
using System.IO;
using System.Configuration;

namespace Samples.Aspnet
{
    // Shows how to use ProtectedConfigurationSection.
    class UsingProtectedConfigurationSection
    {

        static void GetDefaultProvider()
        {
            try
            {
                // Get the application configuration.
                Configuration config =
                    ConfigurationManager.OpenExeConfiguration(
                    ConfigurationUserLevel.None);

                // Get the protected configuration section.
                ProtectedConfigurationSection pcSection =
                    (System.Configuration.ProtectedConfigurationSection)
                    config.GetSection("configProtectedData");

                // Get the current DefaultProvider.
                Console.WriteLine(
                    "Protected configuration section default provider:");
                Console.WriteLine("  {0", pcSection.DefaultProvider);

            
            catch (ConfigurationErrorsException e)
            {
                Console.WriteLine(e.ToString());
            

        


        static void GetProviderCollection()
        {

            try
            {
                // Get the application configuration.
                Configuration config =
                    ConfigurationManager.OpenExeConfiguration(
                    ConfigurationUserLevel.None);

                // Get the protected configuration section.
                ProtectedConfigurationSection pcSection =
                    (System.Configuration.ProtectedConfigurationSection)
                    config.GetSection("configProtectedData");

                Console.WriteLine(
               "Protected configuration section providers:");
                foreach (ProviderSettings ps in
                pcSection.Providers)
                {
                    Console.WriteLine("  {0", ps.Name);
                

            
            catch (ConfigurationErrorsException e)
            {
                Console.WriteLine(e.ToString());
            

        

        public static void Main()
        {
            GetDefaultProvider();
            GetProviderCollection();
        
    
 

System.Object
   System.Configuration.ConfigurationElement
     System.Configuration.ConfigurationSection
      System.Configuration.ProtectedConfigurationSection
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.