Pages

Sunday, February 05, 2006

Decode ASP.NET Page's ViewState

About LosFormatter

Serializes the view state for a Web Forms page. This class cannot be inherited.

The limited object serialization (LOS) formatter is designed for highly compact ASCII format serialization. This class supports serializing any object graph, but is optimized for those containing strings, arrays, and hashtables. It offers second order optimization for many of the .NET primitive types.
This is a private format, and only needs to remain consistent for the lifetime of a Web request. You are not allowed to persist objects serialized with this formatter for any significant length of time.


LosFormatter, a member of the System.Web.UI namespace, is a "stripped down" version of BinaryFormatter that is used internally by the ASP.NET runtime to create and retrieve Page - level objects to / from the hidden _VIEWSTATE field.
LosFormatter, although undocumented, can also be easily used explicitly by the developer. One of the handy features it has is that the serialized string it generates is automatically Base64 encoded so that the ViewState Collection can be persisted through page POSTs. Of course, if you explicitly call the methods of LosFormatter, you are not restricted to keeping the resulting string in the hidden _VIEWSTATE field - you can do whatever you want with it.
Code To Decode Your ViewState :)

System.Web.UI.Triplet _triplet = null;

try
{
if (txt1.Text!="")
{
LosFormatter _losFormatter = new LosFormatter();
System.IO.StringReader _reader= new System.IO.StringReader(txt1.Text);
object _deSerialized = _losFormatter.Deserialize(_reader);
_triplet = _deSerialized as Triplet;
ArrayList _Keys = (((_triplet.Second) as Triplet).First as Pair).First as ArrayList;
ArrayList _Values = (((_triplet.Second) as Triplet).First as Pair).Second as ArrayList;
txt2.Text = "Decoded Values";
txt2.Text = "No. Of Item/s Found : " + _Keys.Count.ToString();
for (int _keysCount =0;_keysCount<=_Keys.Count-1;_keysCount++)
{
txt2.Text +="\r\n" + "Key : " + _Keys[_keysCount].ToString() + "\r\n Value : " + _Values[_keysCount].ToString();
}
}
}
catch(Exception _ex)
{
throw _ex;
}

Wednesday, January 04, 2006