MachineKeySection 類別

定義

定義了控制金鑰產生與演算法的設定,這些設定用於 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 類別的新實例。

屬性

名稱 Description
ApplicationName

取得或設定應用程式名稱。

CompatibilityMode

取得或設定一個值,指定是否使用在 .NET Framework 2.0 Service Pack 1 版本後引入的升級檢視狀態加密方法。

CurrentConfiguration

取得最上層 Configuration 實例的參考,表示目前 ConfigurationElement 實例所屬的組態階層。

(繼承來源 ConfigurationElement)
DataProtectorType

取得或設定資料保護器的名稱。 預設值為 Empty

Decryption

指定用於加密與解密表單認證資料的加密演算法。

DecryptionKey

取得或設定用於加密與解密資料的金鑰,或金鑰產生的過程。

ElementInformation

取得 ElementInformation 物件,其中包含 ConfigurationElement 物件的不可自定義資訊和功能。

(繼承來源 ConfigurationElement)
ElementProperty

取得代表 ConfigurationElementProperty 物件本身的 ConfigurationElement 物件。

(繼承來源 ConfigurationElement)
EvaluationContext

取得 ContextInformation 物件的 ConfigurationElement 物件。

(繼承來源 ConfigurationElement)
HasContext

取得值,這個值表示 CurrentConfiguration 屬性是否 null

(繼承來源 ConfigurationElement)
Item[ConfigurationProperty]

取得或設定這個組態項目的屬性或屬性。

(繼承來源 ConfigurationElement)
Item[String]

取得或設定這個組態專案的屬性、屬性或子專案。

(繼承來源 ConfigurationElement)
LockAllAttributesExcept

取得鎖定屬性的集合。

(繼承來源 ConfigurationElement)
LockAllElementsExcept

取得鎖定專案的集合。

(繼承來源 ConfigurationElement)
LockAttributes

取得鎖定屬性的集合。

(繼承來源 ConfigurationElement)
LockElements

取得鎖定專案的集合。

(繼承來源 ConfigurationElement)
LockItem

取得或設定值,指出專案是否已鎖定。

(繼承來源 ConfigurationElement)
Properties

取得屬性的集合。

(繼承來源 ConfigurationElement)
SectionInformation

取得 SectionInformation 一個包含該物件不可自訂資訊與功能的 ConfigurationSection 物件。

(繼承來源 ConfigurationSection)
Validation

指定用於驗證表單認證及檢視狀態資料的雜湊演算法。

ValidationAlgorithm

取得或設定用於驗證表單驗證及查看狀態資料的演算法名稱。

ValidationKey

取得或設定用於驗證表單認證與檢視狀態資料的金鑰,或是金鑰產生過程。

方法

名稱 Description
DeserializeElement(XmlReader, Boolean)

從組態檔讀取 XML。

(繼承來源 ConfigurationElement)
DeserializeSection(XmlReader)

從組態檔讀取 XML。

(繼承來源 ConfigurationSection)
Equals(Object)

將目前 ConfigurationElement 實例與指定的物件進行比較。

(繼承來源 ConfigurationElement)
GetHashCode()

會獲得代表當前 ConfigurationElement 實例的唯一值。

(繼承來源 ConfigurationElement)
GetRuntimeObject()

在衍生類別中覆寫時,會回傳一個自訂物件。

(繼承來源 ConfigurationSection)
GetTransformedAssemblyString(String)

傳回所指定元件名稱的轉換版本。

(繼承來源 ConfigurationElement)
GetTransformedTypeString(String)

傳回指定之型別名稱的轉換版本。

(繼承來源 ConfigurationElement)
GetType()

取得目前實例的 Type

(繼承來源 Object)
Init()

ConfigurationElement 物件設定為其初始狀態。

(繼承來源 ConfigurationElement)
InitializeDefault()

用來初始化 ConfigurationElement 物件的預設值集。

(繼承來源 ConfigurationElement)
IsModified()

表示此組態元素自最後儲存或載入後是否被修改,且在衍生類別中實作。

(繼承來源 ConfigurationSection)
IsReadOnly()

會取得一個值,表示該物件是否 ConfigurationElement 為唯讀。

(繼承來源 ConfigurationElement)
ListErrors(IList)

將這個 ConfigurationElement 物件和所有子元素中的無效屬性錯誤新增至傳遞的清單。

(繼承來源 ConfigurationElement)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
OnDeserializeUnrecognizedAttribute(String, String)

取得值,指出還原串行化期間是否遇到未知的屬性。

(繼承來源 ConfigurationElement)
OnDeserializeUnrecognizedElement(String, XmlReader)

會獲得一個值,表示在反序列化過程中是否遇到未知元素。

(繼承來源 ConfigurationElement)
OnRequiredPropertyNotFound(String)

找不到必要屬性時擲回例外狀況。

(繼承來源 ConfigurationElement)
PostDeserialize()

在還原串行化之後呼叫。

(繼承來源 ConfigurationElement)
PreSerialize(XmlWriter)

在串行化之前呼叫。

(繼承來源 ConfigurationElement)
Reset(ConfigurationElement)

重置物件的 ConfigurationElement 內部狀態,包括鎖與屬性集合。

(繼承來源 ConfigurationElement)
ResetModified()

在導出類別實作時,將方法的值IsModified()重置為 。false

(繼承來源 ConfigurationSection)
SerializeElement(XmlWriter, Boolean)

當在衍生類別中實作時,會將此組態元素的內容寫入設定檔。

(繼承來源 ConfigurationElement)
SerializeSection(ConfigurationElement, String, ConfigurationSaveMode)

建立一個包含物件未合併視圖 ConfigurationSection 的 XML 字串,作為一個區段來寫入檔案。

(繼承來源 ConfigurationSection)
SerializeToXmlElement(XmlWriter, String)

在衍生類別中實作時,將此組態專案的外部標記寫入組態檔。

(繼承來源 ConfigurationElement)
SetPropertyValue(ConfigurationProperty, Object, Boolean)

將屬性設定為指定的值。

(繼承來源 ConfigurationElement)
SetReadOnly()

設定 IsReadOnly() 物件及所有子元素的 ConfigurationElement 屬性。

(繼承來源 ConfigurationElement)
ShouldSerializeElementInTargetVersion(ConfigurationElement, String, FrameworkName)

表示當設定物件階層序列化至指定目標版本的 .NET 框架時,是否應序列化該元素。

(繼承來源 ConfigurationSection)
ShouldSerializePropertyInTargetVersion(ConfigurationProperty, String, FrameworkName, ConfigurationElement)

指示當設定物件階層序列化至指定版本的 .NET 框架時,是否應序列化該屬性。

(繼承來源 ConfigurationSection)
ShouldSerializeSectionInTargetVersion(FrameworkName)

指示當前ConfigurationSection實例是否應該序列化,當配置物件階層序列化以符合指定目標版本.NET框架時。

(繼承來源 ConfigurationSection)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)
Unmerge(ConfigurationElement, ConfigurationElement, ConfigurationSaveMode)

修改物件 ConfigurationElement 以移除所有不應儲存的值。

(繼承來源 ConfigurationElement)

適用於

另請參閱