![]() |
Web Development »
ASP.NET »
General
Advanced
License: The Code Project Open License (CPOL)
ViewState Serializer, Compressor & EncrypterBy ModMaIt's a very complete and robust processor of ViewState, it allows: to select the way of serialization, compression and encryption optionally. |
C# (C# 2.0), VB (VB 7.x), Windows, .NET (.NET 1.1, .NET 2.0), ASP.NET, Ajax, IIS 5.1, IIS 6, VS.NET2003, VS2005, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
People get frustrated when they see that in their forms, the ViewState is enormous and consumes a bandwidth of madness when being filled with styles, controls, Grids, which results in very long post time at the client. There are many solutions here, from a simple compressor to a storage in Session/Cache.
This code makes work easy, but it is a unique one for a special form: it uses a special serializer to work with binary data.
A point that will interest you is the deficient scope security system of the ViewState. If it is possible to encrypt using a server key, but there are documents that say in a same server with 2 stores online, encrypted ViewStates can be used to cause frauds in the sale of products, the special method with easy code can make a unique key by session difficult to break.
The lite portion of code is based on a simple ViewState compressor: ViewStateCompression.
The compression engine uses the ICSharpCode SharpZipLib.
This code has only been tested in VB 7.1 (VS2003) not in VS2005 platform.
I will not be centered in the class due to lack of time (I will do it), but will explain its conditions of use: (a wonderful way to learn how the code works is by taking a look at the demo;) )
The class can be used in 2 modes: inheritance and a class declaration. I recommend using the inheritance mode, as it is the easiest way.
The inheritance mode is simple, replace:
Public Class formTest1
Inherits System.Web.UI.Page
...
with:
Public Class formTest1
Inherits ViewStateSerializer
...
and simply configure in Page_Load:
SetViewStateValues(EnCrypt As Boolean, Optimize As Boolean)
EnCrypt: If is True, turns on the Encryption algorithms, a random seed & key for each session will be created. Optimize: If is True, turns on the algorithm of Binary Serialization, larger than the other option, but you can add a large DataTable for example in ViewState. The standard deserializer of .NET hands up the server in large DataTables, not this :D Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Response.Expires = -1 'important ?!
If Not IsPostBack Then
SetViewStateValues(True, False) 'Configuration HERE !
...
End If
...
End Sub
The second way is to simply place the code in any location of the Form class. The constructor format is the same as SetViewStateValues:
#Region "Overrides Page: Compression / ViewState Cryptography"
Dim SerialX As New TurboSerializer(True, False)
Protected Overrides Function LoadPageStateFromPersistenceMedium() As Object
Try
Dim viewState As String = Request.Form("__VSTATE")
Return SerialX.DeSerialize(viewState)
Catch
...
Return Nothing
End Try
End Function
Protected Overrides Sub SavePageStateToPersistenceMedium(ByVal viewState As Object)
Try
RegisterHiddenField("__VSTATE", SerialX.Serialize(viewState))
Catch
...
RegisterHiddenField("__VSTATE", String.Empty)
End Try
End Sub
#End Region
You can use deferent configurations in forms but, please use in the Init configuration constant parameters in the same form to prevent browser cache failures (Response.Expires = -1).
Now I write a table to help you to select a ViewState mode that you can use according to your necessities:
| Serialization | Deserialization | Compression | Amount of Data to use | Security | Indicated to: | |
| ViewState normal: | Good | Bad (binary) | None | Use low Data | Low | Forms with low controls, Grids with paging |
| Serializer normal: | Good | Bad (binary) | Good | Mid proposes | Moderate | Grids with Viewstate turned On Without paging |
| Serializer optimized: | Regular | Regular | Regular | Grand Data (DataTable) | Moderate | ViewState with DataTables & Grids with paging or without the ViewState turned off |
This version uses the native compression of VS2005 (no need for SharpZipLib).
The encryption now uses two levels of security, that generate two types of keys (the low mode uses a pseudo-random 3 times at day for updatable keys for all sessions, the high one is the old mode).
V1.1 of this version is compatible with Microsoft Ajax & Microsoft Ajax Control Toolkit (the only one?).
NOTE: This uses a lot of hacks to do it. To do work, see how calls to the code in the overrides section are totally different from the VB 7.x version.
The Optimized mode is hardly tested. I don't check if it works correctly in all cases.
This new version uses a new option to select the MachineKey encryption. No need anymore to set ViewStateEncryptionMode="Never"; CompressPage() now works in Ajax and more optimized De/Serialization.
This new version uses a new API to manage the load & save of ViewState. Now it is more compatible with FW 2.0 & Ajax; please see the annotation code of V1.2 for more information & usage!
Remember that in this version, you must check if ViewStateEncryptionMode="Never" is set to the engine that can compress the ViewState data (encrypting makes a aleatory data that the engine can't compress it!)
If you use the code to compress all pages, you're warned that in Microsoft Ajax, the method response.filter (Async Postback) does not work.
PageStatePersister: more easy, compatible & can use a PageAdapter)| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 26 Sep 2009 Editor: Deeksha Shenoy |
Copyright 2007 by ModMa Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |