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