MachineKeySection 类
定义
定义控制密钥生成和算法的配置设置,这些密钥生成和算法在 Windows Forms 身份验证、视图状态验证和会话状态应用程序隔离中用于加密、解密和消息身份验证代码 (MAC) 操作。Defines the configuration settings that control the key generation and algorithms that are used in encryption, decryption, and message authentication code (MAC) operations in Windows Forms authentication, view-state validation, and session-state application isolation. 此类不能被继承。This class cannot be inherited.
public ref class MachineKeySection sealed : System::Configuration::ConfigurationSection
public sealed class MachineKeySection : System.Configuration.ConfigurationSection
type MachineKeySection = class
inherit ConfigurationSection
Public NotInheritable Class MachineKeySection
Inherits ConfigurationSection
- 继承
示例
本节中的示例演示如何以声明方式为 machineKey 部分的多个属性指定值,也可以将这些属性作为类的成员进行访问 MachineKeySection 。The example in this section demonstrates how to specify values declaratively for several attributes of the machineKey section, which can also be accessed as members of the MachineKeySection class.
以下来自配置文件的示例演示如何以声明方式为 machineKey 节指定值。The following example from a configuration file shows how to specify values declaratively for the machineKey section.
<system.web>
<machineKey validationKey="AutoGenerate,IsolateApps"
decryptionKey="AutoGenerate,IsolateApps"
validation="SHA1"/>
</system.web>
此示例使用 SHA1。This example uses SHA1. 由于与 SHA1 冲突,Microsoft 建议使用 SHA256。Due to collision problems with SHA1, Microsoft recommends SHA256.
下面的示例演示如何 MachineKeySection 在代码中使用类。The following example demonstrates how to use the MachineKeySection class in code.
#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Web;
using System.Web.Configuration;
#endregion
namespace Samples.Aspnet.SystemWebConfiguration
{
class UsingMachineKeySection
{
static void Main(string[] args)
{
try
{
// Set the path of the config file.
string configPath = "";
// Get the Web application configuration object.
Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);
// Get the section related object.
MachineKeySection configSection =
(MachineKeySection)config.GetSection("system.web/machineKey");
// Display title and info.
Console.WriteLine("ASP.NET Configuration Info");
Console.WriteLine();
// Display Config details.
Console.WriteLine("File Path: {0}",
config.FilePath);
Console.WriteLine("Section Path: {0}",
configSection.SectionInformation.Name);
// Display ValidationKey property.
Console.WriteLine("ValidationKey: {0}",
configSection.ValidationKey);
// Set ValidationKey property.
configSection.ValidationKey = "AutoGenerate,IsolateApps";
// Display DecryptionKey property.
Console.WriteLine("DecryptionKey: {0}",
configSection.DecryptionKey);
// Set DecryptionKey property.
configSection.DecryptionKey = "AutoGenerate,IsolateApps";
// Display Validation property.
Console.WriteLine("Validation: {0}",
configSection.Validation);
// Set Validation property.
configSection.Validation = MachineKeyValidation.HMACSHA256;
// Update if not locked.
if (!configSection.SectionInformation.IsLocked)
{
config.Save();
Console.WriteLine("** Configuration updated.");
}
else
{
Console.WriteLine("** Could not update, section is locked.");
}
}
catch (Exception e)
{
// Unknown error.
Console.WriteLine(e.ToString());
}
// Display and wait
Console.ReadLine();
}
}
}
Imports System.Collections.Generic
Imports System.Text
Imports System.Configuration
Imports System.Web
Imports System.Web.Configuration
Namespace Samples.Aspnet.SystemWebConfiguration
Class UsingMachineKeySection
Public Shared Sub Main()
Try
' Set the path of the config file.
Dim configPath As String = ""
' Get the Web application configuration object.
Dim config As System.Configuration.Configuration = _
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(configPath)
' Get the section related object.
Dim configSection As System.Web.Configuration.MachineKeySection = _
CType(config.GetSection("system.web/machineKey"), _
System.Web.Configuration.MachineKeySection)
' Display title and info.
Console.WriteLine("ASP.NET Configuration Info")
Console.WriteLine()
' Display Config details.
Console.WriteLine("File Path: {0}", config.FilePath)
Console.WriteLine("Section Path: {0}", configSection.SectionInformation.Name)
' Display ValidationKey property.
Console.WriteLine("ValidationKey: {0}", _
configSection.ValidationKey)
' Set ValidationKey property.
configSection.ValidationKey = "AutoGenerate,IsolateApps"
' Display DecryptionKey property.
Console.WriteLine("DecryptionKey: {0}", configSection.DecryptionKey)
' Set DecryptionKey property.
configSection.DecryptionKey = "AutoGenerate,IsolateApps"
' Display Validation value.
Console.WriteLine("Validation: {0}", configSection.Validation)
' Set Validation value.
configSection.Validation = MachineKeyValidation.HMACSHA256
' Update if not locked.
If Not configSection.SectionInformation.IsLocked Then
config.Save()
Console.WriteLine("** Configuration updated.")
Else
Console.WriteLine("** Could not update, section is locked.")
End If
Catch e As Exception
' Unknown error.
Console.WriteLine(e.ToString())
End Try
' Display and wait
Console.ReadLine()
End Sub
End Class
End Namespace
注解
MachineKeySection类提供了一种以编程方式访问和修改 MachineKey 配置文件中部分的内容的方法。The MachineKeySection class provides a way to programmatically access and modify the content of the MachineKey section in the configuration file. MachineKey可以在计算机 (Machine.config) 或应用程序 (Web.config) 级别配置节,并控制用于 Windows 窗体身份验证、视图状态验证和会话状态应用程序隔离的密钥和算法。The MachineKey section can be configured at the machine (Machine.config) or application (Web.config) level and controls the keys and algorithms that are used for Windows Forms authentication, view-state validation, and session-state application isolation. 若要在 web 服务器 (的 web 服务器网络上使用任何这些功能) , DecryptionKey ValidationKey MachineKey 必须使用有效的密钥值显式配置部分的和属性。For any of these features to work across a network of Web servers (a Web farm), the DecryptionKey and ValidationKey attributes of the MachineKey section must be configured explicitly and identically with valid key values. 此 AutoGenerate 值对 Web 场不起作用,因为它依赖于密码随机密钥,该密钥是使用计算机本地保护持久保存的,在多台计算机上不会相同。The AutoGenerate value does not work for Web farms, because it relies on a cryptographically random secret, which is persisted using machine-local protection and will not be the same across more than one computer.
继承者说明
如果要在此配置节中指定密钥,在 Web 场方案中,建议使用受保护的配置来加密此部分。If you to specify keys in this configuration section, as is required in Web-farm scenarios, it is recommended that you encrypt this section by using protected configuration.
构造函数
| MachineKeySection() |
使用默认设置初始化 MachineKeySection 类的新实例。Initializes a new instance of the MachineKeySection class by using default settings. |
属性
| ApplicationName |
获取或设置应用程序的名称。Gets or sets the name of the application. |
| CompatibilityMode |
获取或设置一个值,该值指定视图状态的升级加密方法是否在使用 .NET Framework 2.0 版 Service Pack 1 发行版后引入。Gets or sets a value that specifies whether upgraded encryption methods for view state that were introduced after the .NET Framework version 2.0 Service Pack 1 release are used. |
| CurrentConfiguration |
获取对顶级 Configuration 实例的引用,该实例表示当前 ConfigurationElement 实例所属的配置层次结构。Gets a reference to the top-level Configuration instance that represents the configuration hierarchy that the current ConfigurationElement instance belongs to. (继承自 ConfigurationElement) |
| DataProtectorType |
获取或设置数据保护器类型名称。Gets or sets the name of the data protector type. 默认值为 Empty。The default is Empty. |
| Decryption |
指定用于加密和解密 Forms 身份验证数据的加密算法。Specifies the encryption algorithm that is used for encrypting and decrypting forms authentication data. |
| DecryptionKey |
获取或设置用来加密和解密数据的密钥,或生成密钥的过程。Gets or sets the key that is used to encrypt and decrypt data, or the process by which the key is generated. |
| ElementInformation |
获取包含 ConfigurationElement 对象的不可自定义的信息和功能的 ElementInformation 对象。Gets an ElementInformation object that contains the non-customizable information and functionality of the ConfigurationElement object. (继承自 ConfigurationElement) |
| ElementProperty |
获取表示 ConfigurationElement 对象本身的 ConfigurationElementProperty 对象。Gets the ConfigurationElementProperty object that represents the ConfigurationElement object itself. (继承自 ConfigurationElement) |
| EvaluationContext |
获取 ConfigurationElement 对象的 ContextInformation 对象。Gets the ContextInformation object for the ConfigurationElement object. (继承自 ConfigurationElement) |
| HasContext |
获取一个值,该值指示 CurrentConfiguration 属性是否为 |
| Item[ConfigurationProperty] |
获取或设置此配置元素的属性或特性。Gets or sets a property or attribute of this configuration element. (继承自 ConfigurationElement) |
| Item[String] |
获取或设置此配置元素的属性、特性或子元素。Gets or sets a property, attribute, or child element of this configuration element. (继承自 ConfigurationElement) |
| LockAllAttributesExcept |
获取被锁定的特性的集合。Gets the collection of locked attributes. (继承自 ConfigurationElement) |
| LockAllElementsExcept |
获取被锁定的元素的集合。Gets the collection of locked elements. (继承自 ConfigurationElement) |
| LockAttributes |
获取被锁定的特性的集合。Gets the collection of locked attributes. (继承自 ConfigurationElement) |
| LockElements |
获取被锁定的元素的集合。Gets the collection of locked elements. (继承自 ConfigurationElement) |
| LockItem |
获取或设置一个值,该值指示是否已锁定该元素。Gets or sets a value indicating whether the element is locked. (继承自 ConfigurationElement) |
| Properties |
获取属性的集合。Gets the collection of properties. (继承自 ConfigurationElement) |
| SectionInformation |
获取一个 SectionInformation 对象,该对象包含 ConfigurationSection 对象的不可自定义的信息和功能。Gets a SectionInformation object that contains the non-customizable information and functionality of the ConfigurationSection object. (继承自 ConfigurationSection) |
| Validation |
指定用于验证 Forms 身份验证和视图状态数据的哈希算法。Specifies the hashing algorithm that is used for validating forms authentication and view state data. |
| ValidationAlgorithm |
获取或设置指示用于验证 Forms 身份验证和视图状态数据的算法的名称。Gets or sets the name of the algorithm that is used to validate forms authentication and view state data. |
| ValidationKey |
获取或设置键,用来验证 Forms 身份验证和视图状态数据的代码,或生成密钥的过程。Gets or sets the key that is used to validate forms authentication and view state data, or the process by which the key is generated. |
方法
| DeserializeElement(XmlReader, Boolean) |
从配置文件读取 XML。Reads XML from the configuration file. (继承自 ConfigurationElement) |
| DeserializeSection(XmlReader) |
从配置文件读取 XML。Reads XML from the configuration file. (继承自 ConfigurationSection) |
| Equals(Object) |
将当前的 ConfigurationElement 实例与指定的对象进行比较。Compares the current ConfigurationElement instance to the specified object. (继承自 ConfigurationElement) |
| GetHashCode() |
获取表示当前 ConfigurationElement 实例的唯一值。Gets a unique value representing the current ConfigurationElement instance. (继承自 ConfigurationElement) |
| GetRuntimeObject() |
在派生的类中重写时返回自定义对象。Returns a custom object when overridden in a derived class. (继承自 ConfigurationSection) |
| GetTransformedAssemblyString(String) |
返回指定程序集名称的转换版本。Returns the transformed version of the specified assembly name. (继承自 ConfigurationElement) |
| GetTransformedTypeString(String) |
返回指定类型名称的转换版本。Returns the transformed version of the specified type name. (继承自 ConfigurationElement) |
| GetType() |
获取当前实例的 Type。Gets the Type of the current instance. (继承自 Object) |
| Init() |
将 ConfigurationElement 对象设置为其初始状态。Sets the ConfigurationElement object to its initial state. (继承自 ConfigurationElement) |
| InitializeDefault() |
用于初始化 ConfigurationElement 对象的默认值集。Used to initialize a default set of values for the ConfigurationElement object. (继承自 ConfigurationElement) |
| IsModified() |
指示自上次在派生类中实现此配置元素时保存或加载以来是否对其进行过修改。Indicates whether this configuration element has been modified since it was last saved or loaded when implemented in a derived class. (继承自 ConfigurationSection) |
| IsReadOnly() |
获取一个值,该值指示 ConfigurationElement 对象是否为只读。Gets a value indicating whether the ConfigurationElement object is read-only. (继承自 ConfigurationElement) |
| ListErrors(IList) |
将此 ConfigurationElement 对象以及所有子元素中无效属性的错误添加到传递的列表中。Adds the invalid-property errors in this ConfigurationElement object, and in all subelements, to the passed list. (继承自 ConfigurationElement) |
| MemberwiseClone() |
创建当前 Object 的浅表副本。Creates a shallow copy of the current Object. (继承自 Object) |
| OnDeserializeUnrecognizedAttribute(String, String) |
获取一个值,该值指示反序列化过程中是否遇到未知特性。Gets a value indicating whether an unknown attribute is encountered during deserialization. (继承自 ConfigurationElement) |
| OnDeserializeUnrecognizedElement(String, XmlReader) |
获取一个值,该值指示反序列化过程中是否遇到未知元素。Gets a value indicating whether an unknown element is encountered during deserialization. (继承自 ConfigurationElement) |
| OnRequiredPropertyNotFound(String) |
找不到所需属性时引发异常。Throws an exception when a required property is not found. (继承自 ConfigurationElement) |
| PostDeserialize() |
反序列化后调用。Called after deserialization. (继承自 ConfigurationElement) |
| PreSerialize(XmlWriter) |
在序列化之前调用。Called before serialization. (继承自 ConfigurationElement) |
| Reset(ConfigurationElement) |
重置 ConfigurationElement 对象的内部状态,包括锁和属性集合。Resets the internal state of the ConfigurationElement object, including the locks and the properties collections. (继承自 ConfigurationElement) |
| ResetModified() |
在派生类中实现时,将 IsModified() 方法的值重置为 |
| SerializeElement(XmlWriter, Boolean) |
当在派生类中实现后,将此配置元素的内容写入配置文件。Writes the contents of this configuration element to the configuration file when implemented in a derived class. (继承自 ConfigurationElement) |
| SerializeSection(ConfigurationElement, String, ConfigurationSaveMode) |
创建一个包含 ConfigurationSection 对象的分离视图的 XML 字符串,作为单独的节写入到文件中。Creates an XML string containing an unmerged view of the ConfigurationSection object as a single section to write to a file. (继承自 ConfigurationSection) |
| SerializeToXmlElement(XmlWriter, String) |
当在派生类中实现后,将此配置元素的外部标记写入配置文件。Writes the outer tags of this configuration element to the configuration file when implemented in a derived class. (继承自 ConfigurationElement) |
| SetPropertyValue(ConfigurationProperty, Object, Boolean) |
将属性设置为指定值。Sets a property to the specified value. (继承自 ConfigurationElement) |
| SetReadOnly() |
设置 ConfigurationElement 对象及所有子元素的 IsReadOnly() 属性。Sets the IsReadOnly() property for the ConfigurationElement object and all subelements. (继承自 ConfigurationElement) |
| ShouldSerializeElementInTargetVersion(ConfigurationElement, String, FrameworkName) |
指示在为指定的 .NET Framework 目标版本序列化配置对象层次结构时,是否应序列化指定的元素。Indicates whether the specified element should be serialized when the configuration object hierarchy is serialized for the specified target version of the .NET Framework. (继承自 ConfigurationSection) |
| ShouldSerializePropertyInTargetVersion(ConfigurationProperty, String, FrameworkName, ConfigurationElement) |
指示在为指定的 .NET Framework 目标版本序列化配置对象层次结构时,是否应序列化指定属性。Indicates whether the specified property should be serialized when the configuration object hierarchy is serialized for the specified target version of the .NET Framework. (继承自 ConfigurationSection) |
| ShouldSerializeSectionInTargetVersion(FrameworkName) |
指示 ConfigurationSection 在为指定的 .NET Framework 目标版本序列化配置对象层次结构时,是否应序列化当前实例。Indicates whether the current ConfigurationSection instance should be serialized when the configuration object hierarchy is serialized for the specified target version of the .NET Framework. (继承自 ConfigurationSection) |
| ToString() |
返回表示当前对象的字符串。Returns a string that represents the current object. (继承自 Object) |
| Unmerge(ConfigurationElement, ConfigurationElement, ConfigurationSaveMode) |
修改 ConfigurationElement 对象以移除所有不应该保存的值。Modifies the ConfigurationElement object to remove all values that should not be saved. (继承自 ConfigurationElement) |