Page.RegisterRequiresViewStateEncryption 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
向页将控件注册为需要视图状态加密的控件。
public:
void RegisterRequiresViewStateEncryption();
public void RegisterRequiresViewStateEncryption ();
member this.RegisterRequiresViewStateEncryption : unit -> unit
Public Sub RegisterRequiresViewStateEncryption ()
例外
必须在页生命周期中的页 PreRender
阶段之前或期间调用 RegisterRequiresViewStateEncryption() 方法。
示例
下面的代码示例演示如何为 Page 对象设置视图状态加密模式,并通过 请求对视图状态 RegisterRequiresViewStateEncryption进行加密。 在此示例中,从数据库检索客户信息时,视图状态将加密。
<%@ 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 方法将控件注册到页面,并确保控件的视图状态已加密。
如果 设置为 Auto 或 Always,ViewStateEncryptionMode则会加密整个页面状态。