MachineKeySection 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
定义用于控制加密、解密和消息身份验证代码(MAC)操作中Windows 窗体身份验证、视图状态验证和会话状态应用程序隔离中使用的密钥生成和算法的配置设置。 此类不能被继承。
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 进行访问。
配置文件中的以下示例演示如何以声明方式为 machineKey 节指定值。
<system.web>
<machineKey validationKey="AutoGenerate,IsolateApps"
decryptionKey="AutoGenerate,IsolateApps"
validation="SHA1"/>
</system.web>
此示例使用 SHA1。 由于与 SHA1 冲突,Microsoft 建议使用 SHA256。
以下示例演示如何在代码中使用 MachineKeySection 类。
#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 的方法。 可以在计算机(Machine.config)或应用程序(Web.config)级别配置 MachineKey 节,并控制用于Windows 窗体身份验证、视图状态验证和会话状态应用程序隔离的密钥和算法。 若要使其中任何一项功能跨 Web 服务器(Web 场)网络工作,DecryptionKeyValidationKey必须显式配置该部分的属性MachineKey,并且与有效键值完全相同。 该值 AutoGenerate 不适用于 Web 场,因为它依赖于加密随机机密,该机密是使用计算机本地保护持久保存的,并且不会在多台计算机上相同。
继承者说明
如果要在此配置部分中指定密钥,如 Web 场方案中的要求一样,建议使用受保护的配置来加密此部分。
构造函数
| 名称 | 说明 |
|---|---|
| MachineKeySection() |
使用默认设置初始化类的新实例 MachineKeySection 。 |