RsaProtectedConfigurationProvider RsaProtectedConfigurationProvider RsaProtectedConfigurationProvider RsaProtectedConfigurationProvider Class

Definizione

Fornisce un'istanza della classe ProtectedConfigurationProvider che usa la crittografia RSA per crittografare e decrittografare i dati di configurazione.Provides a ProtectedConfigurationProvider instance that uses RSA encryption to encrypt and decrypt configuration data.

public ref class RsaProtectedConfigurationProvider sealed : System::Configuration::ProtectedConfigurationProvider
public sealed class RsaProtectedConfigurationProvider : System.Configuration.ProtectedConfigurationProvider
type RsaProtectedConfigurationProvider = class
    inherit ProtectedConfigurationProvider
Public NotInheritable Class RsaProtectedConfigurationProvider
Inherits ProtectedConfigurationProvider
Ereditarietà
RsaProtectedConfigurationProviderRsaProtectedConfigurationProviderRsaProtectedConfigurationProviderRsaProtectedConfigurationProvider

Esempi

Nell'esempio seguente viene illustrato come utilizzare lo standard RsaProtectedConfigurationProvider per proteggere o rimuovere la protezione di una sezione di configurazione.The following example shows how to use the standard RsaProtectedConfigurationProvider to protect or unprotect a configuration section.

using System;
using System.Configuration;

public class UsingRsaProtectedConfigurationProvider
{

    // Protect the connectionStrings section.
    private static void ProtectConfiguration()
    {

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

        // Define the Rsa provider name.
        string provider =
            "RsaProtectedConfigurationProvider";

        // Get the section to protect.
        ConfigurationSection connStrings =
            config.ConnectionStrings;

        if (connStrings != null)
        {
            if (!connStrings.SectionInformation.IsProtected)
            {
                if (!connStrings.ElementInformation.IsLocked)
                {
                    // 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);
            }
            else
                Console.WriteLine(
                    "Section {0} is already protected by {1}",
                    connStrings.SectionInformation.Name,
                    connStrings.SectionInformation.ProtectionProvider.Name);

        }
        else
            Console.WriteLine("Can't get the section {0}",
                connStrings.SectionInformation.Name);
              
    }


    // Unprotect the connectionStrings section.
    private static void UnProtectConfiguration()
    {

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

        // Get the section to unprotect.
        ConfigurationSection connStrings =
            config.ConnectionStrings;

        if (connStrings != null)
        {
            if (connStrings.SectionInformation.IsProtected)
            {
                if (!connStrings.ElementInformation.IsLocked)
                {
                    // 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);
            }
            else
                Console.WriteLine(
                    "Section {0} is already unprotected.",
                    connStrings.SectionInformation.Name);
                
        }
        else
            Console.WriteLine("Can't get the section {0}",
                connStrings.SectionInformation.Name);

    }


    public static void Main(string[] args)
    {

        string selection = string.Empty;

        if (args.Length == 0)
        {
            Console.WriteLine(
                "Select protect or unprotect");
            return;
        }

        selection = args[0].ToLower();

        switch (selection)
        {
            case "protect":
                ProtectConfiguration();
                break;

            case "unprotect":
                UnProtectConfiguration();
                break;
 
            default:
                Console.WriteLine("Unknown selection");
                break;
        }

        Console.Read();
            
    }


}

Imports System
Imports System.Configuration


Public Class UsingRsaProtectedConfigurationProvider
   
   
   ' 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 Rsa provider name.
        Dim provider As String = _
        "RsaProtectedConfigurationProvider"
      
      ' 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 'UsingRsaProtectedConfigurationProvider

Nell'esempio seguente viene illustrato un estratto un file di configurazione dopo la crittografia.The following example shows an excerpt from a configuration file after encryption.

<?xml version="1.0" encoding="utf-8"?>  
<configuration>  
  <connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">  
    <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"  
        xmlns="http://www.w3.org/2001/04/xmlenc#">  
      <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />  
      <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">  
        <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">  
          <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />  
          <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">  
            <KeyName>Rsa Key</KeyName>  
          </KeyInfo>  
          <CipherData>  
            <CipherValue>B702tRDVHJjC3CYXt7I0ucCDjdht/Vyk/DdUhwQyt7vepSD85dwCP8ox9Y1BUdjajFeTFfFBsGypbli5HPGRYamQdrVkPo07bBBXNT5H02qxREguGUU4iDtV1Xp8BLVZjQMV4ZgP6Wbctw2xRvPC7GvKHLI4fUN/Je5LmutsijA=</CipherValue>  
          </CipherData>  
        </EncryptedKey>  
      </KeyInfo>  
      <CipherData>  
        <CipherValue>ME+XJA2TAj3QN3yT4pJq3sRArC0i7Cz3Da71BkaRe9QNfuVuUjcv0jeGUN4wDdOAZ7LPq6UpVrpirY3kQcALDvPJ5nKxk++Mw75rjtIO8eh2goTY9rCK6zanfzaDshFy7IqItpvs/y2kmij25nM3ury6uO0hCf0UbEL1mbT2jXDqvcrHZUobO1Ef6bygBZ/8HpU+VfF9CTCob/BBE9zUkK37EQhcduwsnzBvDblYbF/Rd+F4lxAkZnecGLfCZjOzJB4xH1a0vvWtPR7zNwL/7I0uHzQjyMdWrkBnotMjoR70R7NELBotCogWO0MBimncKigdR3dTTdrCd72a7UJ4LMlEQaZXGIJp4PIg6qVDHII=</CipherValue>  
      </CipherData>  
    </EncryptedData>  
  </connectionStrings>  
</configuration>  

Commenti

Il RsaProtectedConfigurationProvider classe offre un modo per crittografare informazioni riservate archiviate in un file di configurazione che consente di proteggerli da accessi non autorizzati.The RsaProtectedConfigurationProvider class gives you a way to encrypt sensitive information stored in a configuration file, which helps protect it from unauthorized access. Utilizzare l'elemento predefinito RsaProtectedConfigurationProvider istanza dichiarando il provider e configurare le impostazioni appropriate nel file di configurazione invece di creare un'istanza di questa classe, come illustrato nella sezione esempi.You use the built-in RsaProtectedConfigurationProvider instance by declaring the provider and making appropriate settings in the configuration file instead of creating an instance of this class, as shown in the Examples section.

Il RsaProtectedConfigurationProvider oggetto utilizza le funzioni di crittografia fornite da RSA classe per crittografare e decrittografare le sezioni di configurazione.The RsaProtectedConfigurationProvider object uses the cryptography functions provided by RSA class to encrypt and decrypt configuration sections.

Nota

Prima che ASP.NET può decrittografare le informazioni crittografate nel file di configurazione, l'identità dell'applicazione ASP.NET deve avere accesso in lettura alla chiave di crittografia usata per crittografare e decrittografare i dati di configurazione.Before ASP.NET can decrypt encrypted information in your configuration file, the identity of your ASP.NET application must have read access to the encryption key used to encrypt and decrypt the configuration data. Per altre informazioni, vedere [procedura dettagliata: Crittografia delle informazioni di configurazione tramite configurazione protetta] (https://docs.microsoft.com/previous-versions/dtkwfdky(v=vs.140).For more information, see [Walkthrough: Encrypting Configuration Information Using Protected Configuration](https://docs.microsoft.com/previous-versions/dtkwfdky(v=vs.140).

Costruttori

RsaProtectedConfigurationProvider() RsaProtectedConfigurationProvider() RsaProtectedConfigurationProvider() RsaProtectedConfigurationProvider()

Inizializza una nuova istanza della classe RsaProtectedConfigurationProvider.Initializes a new instance of the RsaProtectedConfigurationProvider class.

Proprietà

CspProviderName CspProviderName CspProviderName CspProviderName

Ottiene il nome del provider del servizio di crittografia (CSP, Cryptographic Service Provider) dell'API di crittografia (CryptoAPI) di Windows.Gets the name of the Windows cryptography API (crypto API) cryptographic service provider (CSP).

Description Description Description Description

Ottiene una breve descrizione di facile comprensione che è possibile visualizzare in strumenti di amministrazione o in altre interfacce utente (UI, User Interface).Gets a brief, friendly description suitable for display in administrative tools or other user interfaces (UIs).

(Inherited from ProviderBase)
KeyContainerName KeyContainerName KeyContainerName KeyContainerName

Ottiene il nome del contenitore di chiavi.Gets the name of the key container.

Name Name Name Name

Ottiene il nome descrittivo utilizzato per fare riferimento al provider durante la configurazione.Gets the friendly name used to refer to the provider during configuration.

(Inherited from ProviderBase)
RsaPublicKey RsaPublicKey RsaPublicKey RsaPublicKey

Ottiene la chiave pubblica usata dal provider.Gets the public key used by the provider.

UseFIPS UseFIPS UseFIPS UseFIPS

Ottiene un valore che indica se il provider usa FIPS.Gets a value indicating whether the provider uses FIPS.

UseMachineContainer UseMachineContainer UseMachineContainer UseMachineContainer

Ottiene un valore che indica se l'oggetto RsaProtectedConfigurationProvider usa il contenitore di chiavi del computer.Gets a value that indicates whether the RsaProtectedConfigurationProvider object is using the machine key container.

UseOAEP UseOAEP UseOAEP UseOAEP

Ottiene un valore che indica se il provider usa i dati di scambio delle chiavi Optimal Asymmetric Encryption Padding (OAEP).Gets a value that indicates whether the provider is using Optimal Asymmetric Encryption Padding (OAEP) key exchange data.

Metodi

AddKey(Int32, Boolean) AddKey(Int32, Boolean) AddKey(Int32, Boolean) AddKey(Int32, Boolean)

Aggiunge una chiave al contenitore di chiavi RSA.Adds a key to the RSA key container.

Decrypt(XmlNode) Decrypt(XmlNode) Decrypt(XmlNode) Decrypt(XmlNode)

Decrittografa il nodo XML passato.Decrypts the XML node passed to it.

DeleteKey() DeleteKey() DeleteKey() DeleteKey()

Rimuove una chiave dal contenitore di chiavi RSA.Removes a key from the RSA key container.

Encrypt(XmlNode) Encrypt(XmlNode) Encrypt(XmlNode) Encrypt(XmlNode)

Crittografa il nodo XML passato.Encrypts the XML node passed to it.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.Determines whether the specified object is equal to the current object.

(Inherited from Object)
ExportKey(String, Boolean) ExportKey(String, Boolean) ExportKey(String, Boolean) ExportKey(String, Boolean)

Esporta una chiave RSA dal contenitore di chiavi.Exports an RSA key from the key container.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Funge da funzione hash predefinita.Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Ottiene l'oggetto Type dell'istanza corrente.Gets the Type of the current instance.

(Inherited from Object)
ImportKey(String, Boolean) ImportKey(String, Boolean) ImportKey(String, Boolean) ImportKey(String, Boolean)

Importa una chiave RSA nel contenitore di chiavi.Imports an RSA key into the key container.

Initialize(String, NameValueCollection) Initialize(String, NameValueCollection) Initialize(String, NameValueCollection) Initialize(String, NameValueCollection)

Inizializza il provider con le impostazioni predefinite.Initializes the provider with default settings.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.Returns a string that represents the current object.

(Inherited from Object)

Sicurezza

SecurityPermission
per l'accesso completo alla risorsa protetta dall'autorizzazione.for full access to the resource protected by the permission. Demand.Demand.

Si applica a

Vedi anche