Page.RegisterRequiresViewStateEncryption Page.RegisterRequiresViewStateEncryption Page.RegisterRequiresViewStateEncryption Page.RegisterRequiresViewStateEncryption Method

定義

向頁面註冊控制項,成為需要檢視狀態加密的控制項。Registers a control with the page as one requiring view-state encryption.

public:
 void RegisterRequiresViewStateEncryption();
public void RegisterRequiresViewStateEncryption ();
member this.RegisterRequiresViewStateEncryption : unit -> unit
Public Sub RegisterRequiresViewStateEncryption ()

例外狀況

在頁面生命週期的 PreRender 階段期間或之前,必須呼叫 RegisterRequiresViewStateEncryption() 方法。The RegisterRequiresViewStateEncryption() method must be called before or during the page PreRender phase in the page life cycle.

範例

下列程式碼範例將示範如何設定檢視狀態的加密模式Page物件,並要求加密透過檢視狀態的RegisterRequiresViewStateEncryptionThe following code example demonstrates setting the view-state encryption mode for a Page object and requesting encryption of the view state through RegisterRequiresViewStateEncryption. 在此範例中,從資料庫擷取客戶資訊時,將會加密檢視狀態。In this example, view state will be encrypted when customer information is retrieved from a database.

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    
    void Page_Load(Object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            if (yesRetrieve.Checked)
            {
                Page.RegisterRequiresViewStateEncryption();
                
                System.Data.SqlClient.SqlConnection conn = 
                    new System.Data.SqlClient.SqlConnection
                    ("server=localhost;database=Northwind;Integrated Security=SSPI");
                System.Data.SqlClient.SqlCommand command =
                    conn.CreateCommand();
                command.CommandText = "Select [CustomerID] From [Customers]";
                conn.Open();
                System.Data.SqlClient.SqlDataReader reader =
                    command.ExecuteReader();
                customerid.Text = reader["CustomerID"].ToString();
                reader.Close();
                conn.Close();
            }
            else
            {
                customerid.Text = "Not retrieved";
            }
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Customer Information</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Customer identifier: 
        <asp:Label ID="customerid" runat="server" Text="Not available" />
        <br />
        Retrieve customer info: 
        <asp:RadioButton ID="yesRetrieve" Text="yes" runat="server" GroupName="group1" /> 
        <asp:RadioButton ID="noRetrieve" Text="no" runat="server" GroupName="group1" />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Submit" />
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">    
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If IsPostBack Then
            
            If (yesRetrieve.Checked) Then
                Page.RegisterRequiresViewStateEncryption()
                
                Dim conn As System.Data.SqlClient.SqlConnection = _
                  New System.Data.SqlClient.SqlConnection _
                  ("server=localhost;database=Northwind;Integrated Security=SSPI")
                Dim command As System.Data.SqlClient.SqlCommand = _
                  conn.CreateCommand()
                command.CommandText = "Select [CustomerID] From [Customers]"
                conn.Open()
                Dim reader As System.Data.SqlClient.SqlDataReader = _
                  command.ExecuteReader()
                customerid.Text = reader("CustomerID").ToString()
                reader.Close()
                conn.Close()
            End If
        End If
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Customer Information</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Customer identifier: 
        <asp:Label ID="customerid" runat="server" Text="Not available" />
        <br />
        Retrieve customer info: 
        <asp:RadioButton ID="yesRetrieve" Text="yes" runat="server" GroupName="group1" /> 
        <asp:RadioButton ID="noRetrieve" Text="no" runat="server" GroupName="group1" />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Submit" />
    </div>
    </form>
</body>
</html>

備註

如果您正在開發的自訂控制項,以處理潛在的敏感資訊,請呼叫RegisterRequiresViewStateEncryption的頁面註冊控制項,並確保已加密的控制項檢視狀態的方法。If you are developing a custom control that deals with potentially sensitive information, call the RegisterRequiresViewStateEncryption method to register the control with the page and ensure view state for the control is encrypted.

將加密整個頁面狀態,如果ViewStateEncryptionMode設定為AutoAlwaysThe entire page state will be encrypted if the ViewStateEncryptionMode is set to Auto or Always.

適用於

另請參閱