共用方式為


SectionInformation.ProtectSection(String) 方法

定義

標示將保護的組態區段。

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

參數

protectionProvider
String

要使用的保護提供者名稱。

例外狀況

AllowLocation 屬性設定為 false

-或-

目標區段已是受保護的資料區段。

範例

下列範例會示範如何使用 ProtectSection 方法。

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

備註

方法 ProtectSection 會將 區段標示為加密,以便以加密形式寫入磁碟上。

預設會包含下列保護提供者:

DpapiProtectedConfigurationProvider

RsaProtectedConfigurationProvider

注意

如果您使用參數或空字串呼叫 ProtectSection 方法 null ,類別 RsaProtectedConfigurationProvider 會當做保護提供者使用。

如需受保護組態的詳細資訊,請參閱 使用受保護的組態加密組態資訊

適用於

另請參閱