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 类将用作保护提供程序。

有关受保护的配置部分的详细信息,请参阅 使用受保护的配置加密配置信息

适用于

另请参阅