MachineKeySection 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
定義組態設定,其在 Windows Form 驗證、檢視狀態驗證及工作階段狀態應用程式隔離中,控制加密、解密和訊息驗證碼 (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 驗證、檢視狀態驗證和會話狀態應用程式隔離的金鑰和演算法。 若要讓上述任一功能在 Web 伺服器網路中運作, (Web 伺服器陣列) ,DecryptionKey
區段的 MachineKey
和 ValidationKey
屬性必須明確且與有效的索引鍵值相同。 此值 AutoGenerate
不適用於 Web 伺服器數位,因為它依賴密碼編譯隨機密碼,這是使用電腦本機保護保存,而且不會在多部電腦上相同。
給繼承者的注意事項
如果您要在此組態區段中指定密鑰,如 Web 伺服器陣列案例中的必要專案,建議您使用受保護的組態來加密此區段。
建構函式
MachineKeySection() |
使用預設值初始化 MachineKeySection 類別的新執行個體。 |