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 Forms驗證、視圖狀態驗證及會話狀態應用程式隔離的金鑰與演算法。 若要在網路伺服器網路(網路農場DecryptionKey)上運作,該區段的ValidationKey屬性MachineKey必須明確且相同地設定,並設定有效的鍵值。 這個 AutoGenerate 值對網路農場不適用,因為它依賴於一個密碼學上隨機的秘密,且該秘密會透過機器本地保護來持久化,且在多台電腦間不會相同。
給繼承者的注意事項
若你在此設定區段指定金鑰,如網路農場情境所要求,建議使用受保護設定加密此區。
建構函式
| 名稱 | Description |
|---|---|
| MachineKeySection() |
使用預設設定初始化該 MachineKeySection 類別的新實例。 |