ProtectedConfigurationSection 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供對 configProtectedData
組態區段的程式方式存取。 此類別無法獲得繼承。
public ref class ProtectedConfigurationSection sealed : System::Configuration::ConfigurationSection
public sealed class ProtectedConfigurationSection : System.Configuration.ConfigurationSection
type ProtectedConfigurationSection = class
inherit ConfigurationSection
Public NotInheritable Class ProtectedConfigurationSection
Inherits ConfigurationSection
- 繼承
範例
下列組態檔摘要說明如何以宣告方式指定受保護的數據提供者。
<configProtectedData defaultProvider="RsaProtectedConfigurationProvider">
<providers>
<clear />
<add keyContainerName="NetFrameworkConfigurationKey" cspProviderName="" useMachineContainer="true" useOAEP="false" description="Uses RsaCryptoServiceProvider to encrypt and decrypt" name="RsaProtectedConfigurationProvider" type="System.Configuration.RsaProtectedConfigurationProvider,System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<add useMachineProtection="true" description="Uses CryptProtectData and CryptUnProtectData Windows APIs to encrypt and decrypt" keyEntropy="" name="DataProtectionConfigurationProvider" type="System.Configuration.DpapiProtectedConfigurationProvider,System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
</configProtectedData>
下列程式代碼範例示範如何使用 ProtectedConfigurationSection 類別,以程式設計方式存取組態檔區段中的值 configProtectedData
。
using System;
using System.IO;
using System.Configuration;
namespace Samples.Aspnet
{
// Shows how to use ProtectedConfigurationSection.
class UsingProtectedConfigurationSection
{
static void GetDefaultProvider()
{
try
{
// Get the application configuration.
Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
// Get the protected configuration section.
ProtectedConfigurationSection pcSection =
(System.Configuration.ProtectedConfigurationSection)
config.GetSection("configProtectedData");
// Get the current DefaultProvider.
Console.WriteLine(
"Protected configuration section default provider:");
Console.WriteLine(" {0}", pcSection.DefaultProvider);
}
catch (ConfigurationErrorsException e)
{
Console.WriteLine(e.ToString());
}
}
static void GetProviderCollection()
{
try
{
// Get the application configuration.
Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
// Get the protected configuration section.
ProtectedConfigurationSection pcSection =
(System.Configuration.ProtectedConfigurationSection)
config.GetSection("configProtectedData");
Console.WriteLine(
"Protected configuration section providers:");
foreach (ProviderSettings ps in
pcSection.Providers)
{
Console.WriteLine(" {0}", ps.Name);
}
}
catch (ConfigurationErrorsException e)
{
Console.WriteLine(e.ToString());
}
}
public static void Main()
{
GetDefaultProvider();
GetProviderCollection();
}
}
}
Imports System.IO
Imports System.Configuration
' Shows how to use ProtectedConfigurationSection.
Class UsingProtectedConfigurationSection
Shared Sub GetDefaultProvider()
Try
' Get the application configuration.
Dim config As Configuration = _
ConfigurationManager.OpenExeConfiguration( _
ConfigurationUserLevel.None)
' Get the protected configuration section.
Dim pcSection _
As ProtectedConfigurationSection = _
CType(config.GetSection( _
"configProtectedData"), _
System.Configuration.ProtectedConfigurationSection)
' Get the current DefaultProvider.
Console.WriteLine( _
"Protected configuration section default provider:")
Console.WriteLine("{0}", _
pcSection.DefaultProvider)
Catch e As ConfigurationErrorsException
Console.WriteLine(e.ToString())
End Try
End Sub
Shared Sub GetProviderCollection()
Try
' Get the application configuration.
Dim config As Configuration = _
ConfigurationManager.OpenExeConfiguration( _
ConfigurationUserLevel.None)
' Get the protected configuration section.
Dim pcSection _
As ProtectedConfigurationSection = _
CType(config.GetSection( _
"configProtectedData"), _
System.Configuration.ProtectedConfigurationSection)
Console.WriteLine( _
"Protected configuration section providers:")
Dim ps As ProviderSettings
For Each ps In pcSection.Providers
Console.WriteLine(" {0}", ps.Name)
Next ps
Catch e As ConfigurationErrorsException
Console.WriteLine(e.ToString())
End Try
End Sub
Public Shared Sub Main()
GetDefaultProvider()
GetProviderCollection()
End Sub
End Class
備註
組 configProtectedData
態檔區段在其 元素中包含 providers
受保護數據提供者的集合。
注意
您可以使用 Aspnet_regiis.exe 工具來加密和解密組態區段。 請參閱 使用受保護組態將組態資訊加密。
建構函式
ProtectedConfigurationSection() |
使用預設值初始化 ProtectedConfigurationSection 類別的新執行個體。 |