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 类的新实例。 |