Viewstate decoder Analyzer

Source: Internet
Author: User

 

 

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; using system. io; using system. web. ui; using system. collections; using system. XML; using system. collections. specialized; using system. text. regularexpressions; namespace viewstate decoding analyzer {public partial class form1: FORM {textbox Tex Tbox = NULL; Public form1 () {initializecomponent ();} private void txtviewstate_leave (Object sender, eventargs e) {textbox = (textbox) sender;} private void splitcontainer=splittermoved (Object sender, splittereventargs e) {If (textbox = NULL) {textbox = txtviewstate;} textbox. focus () ;}# region decodes and analyzes the input viewstate string + private void txtdecoder_click (Object sender, eventargs e) Private void t Xtdecoder_click (Object sender, eventargs e) {xmldocument document = NULL; string Vs = get _ viewcodestring (txtviewstate. text. trim (); // obtain the viewstate string if (string. isnullorempty (VS) return; byte [] buff = encoding. utf8.getbytes (VS); stream = new memorystream (buff); try {losformatter LF = new losformatter (); object allviewstate = lf. deserialize (Stream); // decode xmldocument dom = viewstatexmlbu Ilder. buildxml (allviewstate); stringbuilder sb = new stringbuilder (); Using (stringwriter writer = new stringwriter (SB) {Dom. save (writer);} txtresult. TEXT = sb. tostring ();} catch (exception ER) {MessageBox. show (ER. message, "Resolution error", messageboxbuttons. OK, messageboxicon. stop) ;}# endregion # region simplifies display of viewstate decoded content + private void btnsimple_click (Object sender, eventargs e) Private void btnsi Mple_click (Object sender, eventargs e) {string S = RegEx. replace (txtresult. text, "<[^>] +>", ""); txtresult. TEXT = RegEx. replace (S ,@"(? M) ^ \ s + {1} quot;, "\ r \ n ");} # endregion # set the minimum panel2 width + private void form1_load (Object sender, eventargs e) Private void form1_load (Object sender, eventargs e) {splitcontainer1.panel2minsize = 430; // set the minimum width of panel2} # endregion # region obtain the correct viewstate code and private string get _ viewcodestring (string S) /// <summary> /// obtain the viewstate of the correct code to be decoded /// </Summary> /// <Param name = "S"> may contain "extra quotation marks "content "Vie Wstate string </param> // <returns> </returns> private string get _ viewcodestring (string s) {int p1 = S. indexof ('"'); If (P1> = 0) // If double quotation marks are not included, the entire string is taken {int P2 = S. indexof ('"', P1 + 1); If (P2> 0) // if there is a pair of double quotation marks, take the stuff inside the double quotation marks {S = S. substring (P1 + 1, P2-P1-1);} else // if there is only one double quotation mark, take the long string on both sides of the quotation mark {string S1 = S. substring (0, P1 ). trim (); string S2 = S. substring (P1 + 1 ). trim (); s = s1.length> s2.length? S1: S2 ;}}return s ;}# endregion }# region class viewstatexmlbuilder, create viewstate xmldocument public class viewstatexmlbuilder {public static xmldocument buildxml (Object Tree) {xmldocument dom = new xmldocument (); Dom. appendchild (Dom. createelement ("viewstate"); buildelement (DOM, Dom. documentelement, tree); Return Dom;} Private Static void buildelement (xmldocument Dom, xmlelement ELEM, Object Tree Node) {If (treenode! = NULL) {xmlelement element; type = treenode. getType (); If (type = typeof (pair) {element = Dom. createelement (getpolictypename (treenode); ELEM. appendchild (element); buildelement (DOM, element, (pair) treenode ). first); buildelement (DOM, element, (pair) treenode ). second);} else if (type = typeof (triplet) {element = Dom. createelement (getpolictypename (treenode); ELEM. appendchild (element); buildelement (DOM, element, (triplet) treenode ). first); buildelement (DOM, element, (triplet) treenode ). second); buildelement (DOM, element, (triplet) treenode ). third);} else if (type = typeof (arraylist) {element = Dom. createelement (getpolictypename (treenode); ELEM. appendchild (element); foreach (Object OBJ in (arraylist) treenode) {buildelement (DOM, element, OBJ) ;}} else if (treenode is array) {element = Dom. createelement ("array"); ELEM. appendchild (element); foreach (Object OBJ in (array) treenode) {buildelement (DOM, element, OBJ) ;}} else if (treenode is hybriddictionary) {element = Dom. createelement (getpolictypename (treenode); Dom. documentelement. appendchild (element); foreach (Object OBJ in (hybriddictionary) treenode) {buildelement (DOM, element, OBJ) ;}} else if (treenode is dictionaryentry) {element = Dom. createelement (getpolictypename (treenode); ELEM. appendchild (element); dictionaryentry entry = (dictionaryentry) treenode; buildelement (DOM, element, entry. key); buildelement (DOM, element, entry. value);} else {element = Dom. createelement (getpolictypename (treenode); If (type = typeof (indexedstring) {element. innertext = (indexedstring) treenode ). value;} else {element. innertext = treenode. tostring ();} ELEM. appendchild (element) ;}} Private Static string getpolictypename (Object OBJ) {string STR = obj. getType (). tostring (); Return Str. substring (Str. lastindexof (". ") + 1) ;}# endregion}

 

Through analysis and research on the page class, we have a better understanding of the viewstate organizational structure. Compile this program to facilitate analysis of viewstste and effectively control the file size.

 

Some other problems may exist. Currently, the following problems are found:

 

In the original developed web system, for example, vs2003 and 2005, viewstate may not be decoded. Is the viewstate generated between different vs versions slightly different?

 

Study again if you have time
The losformatte. deserialize () method is further explained.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.