DpapiProtectedConfigurationProvider Class

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

Provides a ProtectedConfigurationProvider object that uses the Windows data protection API (DPAPI) to encrypt and decrypt configuration data.

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

'Declaration
Public NotInheritable Class DpapiProtectedConfigurationProvider
	Inherits ProtectedConfigurationProvider
'Usage
Dim instance As DpapiProtectedConfigurationProvider

public final class DpapiProtectedConfigurationProvider extends ProtectedConfigurationProvider
public final class DpapiProtectedConfigurationProvider extends ProtectedConfigurationProvider

The DpapiProtectedConfigurationProvider allows you to protect sensitive information stored in a configuration file from unauthorized access.

You use the standard DpapiProtectedConfigurationProvider by declaring the provider and appropriate settings in the configuration file rather than creating an instance of this class. Refer to the next example.

For more information about Protected Configuration, see Encrypting Configuration Information Using Protected Configuration.

The DpapiProtectedConfigurationProvider uses the Windows built-in cryptographic services and can be configured for either machine-specific or user-account-specific protection. Machine-specific protection is useful for anonymous services but provides less security. User-account-specific protection can be used with services that run with a specific user identity.

The following example shows how to use the standard DpapiProtectedConfigurationProvider to protect or unprotect a configuration section.


Imports System
Imports System.Configuration


Public Class UsingDpapiProtectedConfigurationProvider


    ' Protect the connectionStrings section.
    Private Shared Sub ProtectConfiguration()

        ' Get the application configuration file.
        Dim config As System.Configuration.Configuration = _
        ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

        ' Define the Dpapi provider name.
        Dim provider As String = _
            "DataProtectionConfigurationProvider"


        ' Get the section to protect.
        Dim connStrings As ConfigurationSection = _
        config.ConnectionStrings

        If Not (connStrings Is Nothing) Then
            If Not connStrings.SectionInformation.IsProtected Then
                If Not connStrings.ElementInformation.IsLocked Then
                    ' Protect the section.

                    connStrings.SectionInformation.ProtectSection(provider)


                    connStrings.SectionInformation.ForceSave = True

                    config.Save(ConfigurationSaveMode.Full)

                    Console.WriteLine( _
                    "Section {0 is now protected by {1", _
                    connStrings.SectionInformation.Name, _
                    connStrings.SectionInformation.ProtectionProvider.Name)

                Else
                    Console.WriteLine( _
                    "Can't protect, section {0 is locked", _
                    connStrings.SectionInformation.Name)
                End If
            Else
                Console.WriteLine( _
                "Section {0 is already protected by {1", _
                connStrings.SectionInformation.Name, _
                connStrings.SectionInformation.ProtectionProvider.Name)
            End If

        Else
            Console.WriteLine( _
            "Can't get the section {0", _
            connStrings.SectionInformation.Name)
        End If
    End Sub 'ProtectConfiguration



    ' Unprotect the connectionStrings section.
    Private Shared Sub UnProtectConfiguration()

        ' Get the application configuration file.
        Dim config As System.Configuration.Configuration = _
        ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

        ' Get the section to unprotect.
        Dim connStrings As ConfigurationSection = _
        config.ConnectionStrings

        If Not (connStrings Is Nothing) Then
            If connStrings.SectionInformation.IsProtected Then
                If Not connStrings.ElementInformation.IsLocked Then
                    ' Unprotect the section.
                    connStrings.SectionInformation.UnprotectSection()

                    connStrings.SectionInformation.ForceSave = True
                    config.Save(ConfigurationSaveMode.Full)

                    Console.WriteLine( _
                    "Section {0 is now unprotected.", _
                    connStrings.SectionInformation.Name)

                Else
                    Console.WriteLine( _
                    "Can't unprotect, section {0 is locked", _
                    connStrings.SectionInformation.Name)
                End If
            Else
                Console.WriteLine( _
                "Section {0 is already unprotected.", _
                connStrings.SectionInformation.Name)
            End If

        Else
            Console.WriteLine( _
            "Can't get the section {0", _
            connStrings.SectionInformation.Name)
        End If
    End Sub 'UnProtectConfiguration



    Public Shared Sub Main(ByVal args() As String)

        Dim selection As String = String.Empty

        If args.Length = 0 Then
            Console.WriteLine( _
            "Select protect or unprotect")
            Return
        End If

        selection = args(0).ToLower()

        Select Case selection
            Case "protect"
                ProtectConfiguration()

            Case "unprotect"
                UnProtectConfiguration()

            Case Else
                Console.WriteLine( _
                "Unknown selection")
        End Select

        Console.Read()
    End Sub 'Main

End Class 'UsingDpapiProtectedConfigurationProvider


The following configuration excerpts show the configuration section before and after the protection has been applied.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="NorthwindConnectionString" 
      connectionString="Data Source=webnetue2;Initial Catalog=Northwind;User ID=aspnet_test;Password=test"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <EncryptedData>
      <CipherData>                <CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAcAMh0jIC1kigyFfd9AUZfQQAAAACAAAAAAADZgAAqAAAABAAAADQwbQ2DgIgIlqskE1RI9UpAAAAAASAAACgAAAAEAAAAAXlYBxi3jhM6wv4sxLhugsQAgAAgoReHZS2406dc/AyRDd6WuNr4ihHn6fbipd4tzHEmeuyS4o4fS4CmT3jMt/WjsP/kR7TF4ygwr2GG47podK79ECpVCZHAgctCauCYjE2Ls3iphKXy/pHic2o6aaClt/xPm+fb4OfODv6XjrJhJzGK2lqUPXkyJN1w2zwh6OVpDQF9N8vTyxL4eitp35/M5zYbW7e6VVAgYUOxlNxgCV5+jXpUKh/rPovopTD392u8KavqQFW1iu+gBPSPq/xeZNz+qYMKbUl+r4VTzBQg3fPlRxp1lNZmM2yRgUbkYPNaFb9ihS7GAg5/wZn8lLmThvq39eA0Vlp6hDE92iop885umELt0/NBKf5umQCqqz9EXXLbmmGc7qoLqTaYVuOmqx0LsvrJL0wSL1dSySCjmB/dNAtVUYgg02eWQNKyaLqnpMdCbTLLQ/oCKuNkL5OQ7t1yl5wQGjQhieIRzLtrMgpTSyaHbqDsRurp9Bc5mM078IAg1hXquQNKlJC/wiJ9kbHerFCbtuLGy/7nXVrFH91ud4U4ExCJEuhoTdmuql5kbqYd6Ye/bu2CftPni19nDkSJ8w4NoqMNKbK3Mi/Cd0o113HsVYlETMv1vlJWZWYP91PK9trixiY4E0G81c6IKITjHDrOJ9evdw2T1/TrvY6pzre3UXSJbFMDQVX6JoAxFk02SRZDKOZdRojeoX19lgrFAAAABzjlz3Qg2as3vn7MRQVxDfZucgE</CipherValue>
      </CipherData>
    </EncryptedData>
  </connectionStrings>
  <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>
</configuration>

System.Object
   System.Configuration.Provider.ProviderBase
     System.Configuration.ProtectedConfigurationProvider
      System.Configuration.DpapiProtectedConfigurationProvider

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

Community Additions

ADD
Show: