SectionInformation.ProtectSection(String) Metoda

Definicja

Oznacza sekcję konfiguracji ochrony.

public:
 void ProtectSection(System::String ^ protectionProvider);
public void ProtectSection (string protectionProvider);
member this.ProtectSection : string -> unit
Public Sub ProtectSection (protectionProvider As String)

Parametry

protectionProvider
String

Nazwa dostawcy ochrony do użycia.

Wyjątki

Właściwość AllowLocation ma ustawioną wartość false.

-lub-

Sekcja docelowa jest już sekcją chronionych danych.

Przykłady

W poniższym przykładzie pokazano, jak używać ProtectSection metody .

static public void ProtectSection()
{

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

    // Get the section.
    UrlsSection section =
        (UrlsSection)config.GetSection("MyUrls");

    // Protect (encrypt)the section.
    section.SectionInformation.ProtectSection(
        "RsaProtectedConfigurationProvider");

    // Save the encrypted section.
    section.SectionInformation.ForceSave = true;

    config.Save(ConfigurationSaveMode.Full);

    // Display decrypted configuration 
    // section. Note, the system
    // uses the Rsa provider to decrypt
    // the section transparently.
    string sectionXml =
        section.SectionInformation.GetRawXml();

    Console.WriteLine("Decrypted section:");
    Console.WriteLine(sectionXml);
}
Public Shared Sub ProtectSection() 
    
    ' Get the current configuration file.
    Dim config _
    As System.Configuration.Configuration = _
    ConfigurationManager.OpenExeConfiguration( _
    ConfigurationUserLevel.None)

    
    ' Get the section.
    Dim section As UrlsSection = _
    CType(config.GetSection("MyUrls"), UrlsSection)
    
    
    ' Protect (encrypt)the section.
    section.SectionInformation.ProtectSection( _
    "RsaProtectedConfigurationProvider")
    
    ' Save the encrypted section.
    section.SectionInformation.ForceSave = True
    
    config.Save(ConfigurationSaveMode.Full)
    
    ' Display decrypted configuration 
    ' section. Note, the system
    ' uses the Rsa provider to decrypt
    ' the section transparently.
    Dim sectionXml As String = _
    section.SectionInformation.GetRawXml()
    
    Console.WriteLine("Decrypted section:")
    Console.WriteLine(sectionXml)

End Sub

Uwagi

Metoda ProtectSection oznacza sekcję szyfrowania, aby została zapisana w postaci zaszyfrowanej na dysku.

Domyślnie są dołączani następujący dostawcy ochrony:

DpapiProtectedConfigurationProvider

RsaProtectedConfigurationProvider

Uwaga

Jeśli wywołasz metodę ProtectSection z parametrem lub pustym null ciągiem, RsaProtectedConfigurationProvider klasa jest używana jako dostawca ochrony.

Aby uzyskać więcej informacji na temat sekcji konfiguracji chronionej, zobacz Szyfrowanie informacji o konfiguracji przy użyciu konfiguracji chronionej.

Dotyczy

Zobacz też