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s+Pjs+Ozs+O3Q8cDxwPGw8VGV4dDs+O2w8Jm5ic3BcOzs+Pjs+Ozs+O3Q8cDxwPGw8VGV
4dDs+O2w8RnJhbmNlOz4+Oz47Oz47Pj47Pj47Pj47Pj47Pj47Pg==" />
Yikes! By simply disabling ViewState for the grid, the payload size for the same page becomes dramatically smaller: <HTML>
<HEAD>
<title>Reducing Page "HTML Payload"</title>
</HEAD>
<body>
<form name="_ctl0" method="post" action="lessviewstate.aspx" id="_ctl0">
<input type="hidden" name="__VIEWSTATE" value="dDwxNTgzOTU2ODA7Oz4=" />
Here''''s the complete LessViewState code in Visual Basic and C#:
[Visual Basic]<%@ Import Namespace="System.Data" %>
<html>
<HEAD>
<title>Reducing Page "HTML Payload"</title>
</HEAD>
<body>
<form runat="server">
<H3>
Reducing Page "HTML Payload" by Disabling ViewState
</H3>
<P>
<asp:datagrid id="DataGrid1" runat="server" EnableViewState="false"
BorderStyle="None" BorderWidth="1px" BorderColor="#CCCCCC"
BackColor="White" CellPadding="5">
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#006699">
</HeaderStyle>
</asp:datagrid>
</P>
</form>
</body>
</html><script runat="server">
Private Sub Page_Load(sender As Object, e As EventArgs)
Dim ds as New DataSet()
ds.ReadXml(Server.MapPath("TestData.xml"))
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End Sub
</script>
[C#]<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<html>
<HEAD>
<title>Reducing Page "HTML Payload"</title>
</HEAD>
<body>
<form runat="server">
<H3>
Reducing Page "HTML Payload" by Disabling ViewState
</H3>
<P>
<asp:datagrid id="DataGrid1" runat="server" EnableViewState="false"
BorderStyle="None" BorderWidth="1px" BorderColor="#CCCCCC"
BackColor="White" CellPadding="5">
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#006699">
</HeaderStyle>
</asp:datagrid>
</P>
</form>
</body>
</html>
<script runat="server">
void Page_Load(object sender, EventArgs e) {
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("TestData.xml"));
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
</script>
Disabling ViewState
In the example above, I disabled ViewState for the grid by setting its EnableViewState property to false. ViewState can be disabled for a single control, for an entire page, or for an entire application, as follows:
| Per control (on tag) |
<asp:datagrid EnableViewState="false" ... />
Per page (in directive) |
<%@ Page EnableViewState="False" ... %>
Per application (in web.config) |
<Pages EnableViewState="false" ... />
Making ViewState More Secure
Because it''''s not formatted as clear text, folks sometimes assume that ViewState is encrypted—it''''s not. Instead, ViewState is merely base64-encoded to ensure that values are not altered during a roundtrip, regardless of the response/request encoding used by the application.
There are two levels of ViewState security you may wish to add to your application:
- Tamper-proofing
- Encryption
It''''s important to note that ViewState security has a direct effect on the time required to process and render an ASP.NET page. In short, more secure is slower, so don''''t add security to ViewState if you don''''t need it.
Tamper-Proofing
A hashcode will not secure the actual data within the ViewState field, but it will greatly reduce the likelihood of someone tampering with ViewState to try to spoof your application, that is, posting back values that your application would normally prevent a user from inputting.
You can instruct ASP.NET to append a hashcode to the ViewState field by setting the EnableViewStateMAC attribute:
<%@Page EnableViewStateMAC=true %>
EnableViewStateMAC can be set at the page or application level. Upon postback, ASP.NET will generate a hashcode for the ViewState data and compare it to the hashcode store in the posted value. If they don''''t match, the ViewState data will be discarded and the controls will revert to their original settings.
By default, ASP.NET generates the ViewState hashcode using the SHA1 algorithm. Alternatively, you can select the MD5 algorithm by setting <machineKey> in the machine.config file as follows:
<machineKey validation="MD5" />
Encryption
You can use encryption to protect the actual data values within the ViewState field. First, you must set EnableViewStatMAC="true", as above. Then, set the machineKey validation type to 3DES. This instructs ASP.NET to encrypt the ViewState value using the Triple DES symmetric encryption algorithm.
<machineKey validation="3DES" />
ViewState Security on a Web Farm
By default, ASP.NET creates a random validation key and stores it in each server''''s Local Security Authority (LSA). In order to validate a ViewState field created on another server, the validationKey for both servers must be set to the same value. If you secure ViewState by any of the means listed above for an application running in a Web Farm configuration, you will need to provide a single, shared validation key for all of the servers.
The validation key is a string of 20 to 64 random, cryptographically-strong bytes, represented as 40 to 128 hexadecimal characters. Longer is more secure, so a 128-character key is recommended for machines that support it. For example:
<machineKey validation="SHA1" validationKey="
F3690E7A3143C185AB1089616A8B4D81FD55DD7A69E上一页 [1] [2] [3] [4] 下一页