Page.RegisterRequiresViewStateEncryption Metodo

Definizione

Registra un controllo con la pagina in modo che richieda la crittografia dello stato di visualizzazione.

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

Eccezioni

Il metodo RegisterRequiresViewStateEncryption() deve essere chiamato prima o durante la fase PreRender della pagina del ciclo di vita della pagina.

Esempio

Nell'esempio di codice seguente viene illustrata l'impostazione della modalità di crittografia dello stato di visualizzazione per un Page oggetto e la richiesta di crittografia dello stato di visualizzazione tramite RegisterRequiresViewStateEncryption. In questo esempio lo stato di visualizzazione verrà crittografato quando vengono recuperate informazioni sui clienti da un 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>

Commenti

Se si sta sviluppando un controllo personalizzato che gestisce informazioni potenzialmente sensibili, chiamare il RegisterRequiresViewStateEncryption metodo per registrare il controllo con la pagina e assicurarsi che lo stato di visualizzazione per il controllo sia crittografato.

L'intero stato della pagina verrà crittografato se l'oggetto ViewStateEncryptionMode è impostato su Auto o Always.

Si applica a

Vedi anche