Share via


SectionInformation.ProtectSection(String) Metode

Definisi

Menandai bagian konfigurasi untuk perlindungan.

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

Parameter

protectionProvider
String

Nama penyedia perlindungan yang akan digunakan.

Pengecualian

Properti AllowLocation diatur ke false.

-atau-

Bagian target sudah menjadi bagian data yang dilindungi.

Contoh

Contoh berikut menunjukkan cara menggunakan ProtectSection metode .

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

Keterangan

Metode menandai ProtectSection bagian untuk enkripsi sehingga akan ditulis dalam bentuk terenkripsi pada disk.

Penyedia perlindungan berikut disertakan secara default:

DpapiProtectedConfigurationProvider

RsaProtectedConfigurationProvider

Catatan

Jika Anda memanggil ProtectSection metode dengan null parameter atau string kosong, RsaProtectedConfigurationProvider kelas digunakan sebagai penyedia perlindungan.

Untuk informasi selengkapnya tentang bagian konfigurasi yang dilindungi, lihat Mengenkripsi Informasi Konfigurasi Menggunakan Konfigurasi Terproteksi.

Berlaku untuk

Lihat juga