How to decode viewstate

前端 未结 11 1294
终归单人心
终归单人心 2020-11-28 04:45

I need to see the contents of the viewstate of an asp.net page. I looked for a viewstate decoder, found Fridz Onion\'s ViewState Decoder but it asks for the url of a page to

11条回答
  •  误落风尘
    2020-11-28 05:19

    Here is the source code for a ViewState visualizer from Scott Mitchell's article on ViewState (25 pages)

    using System;
    using System.Collections;
    using System.Text;
    using System.IO;
    using System.Web.UI;
    
    
    namespace ViewStateArticle.ExtendedPageClasses
    {
        /// 
        /// Parses the view state, constructing a viaully-accessible object graph.
        /// 
        public class ViewStateParser
        {
            // private member variables
            private TextWriter tw;
            private string indentString = "   ";
    
            #region Constructor
            /// 
            /// Creates a new ViewStateParser instance, specifying the TextWriter to emit the output to.
            /// 
            public ViewStateParser(TextWriter writer)
            {
                tw = writer;
            }
            #endregion
    
            #region Methods
            #region ParseViewStateGraph Methods
            /// 
            /// Emits a readable version of the view state to the TextWriter passed into the object's constructor.
            /// 
            /// The view state object to start parsing at.
            public virtual void ParseViewStateGraph(object viewState)
            {
                ParseViewStateGraph(viewState, 0, string.Empty);    
            }
    
            /// 
            /// Emits a readable version of the view state to the TextWriter passed into the object's constructor.
            /// 
            /// A base-64 encoded representation of the view state to parse.
            public virtual void ParseViewStateGraph(string viewStateAsString)
            {
                // First, deserialize the string into a Triplet
                LosFormatter los = new LosFormatter();
                object viewState = los.Deserialize(viewStateAsString);
    
                ParseViewStateGraph(viewState, 0, string.Empty);    
            }
    
            /// 
            /// Recursively parses the view state.
            /// 
            /// The current view state node.
            /// The "depth" of the view state tree.
            /// A label to display in the emitted output next to the current node.
            protected virtual void ParseViewStateGraph(object node, int depth, string label)
            {
                tw.Write(System.Environment.NewLine);
    
                if (node == null)
                {
                    tw.Write(String.Concat(Indent(depth), label, "NODE IS NULL"));
                } 
                else if (node is Triplet)
                {
                    tw.Write(String.Concat(Indent(depth), label, "TRIPLET"));
                    ParseViewStateGraph(((Triplet) node).First, depth+1, "First: ");
                    ParseViewStateGraph(((Triplet) node).Second, depth+1, "Second: ");
                    ParseViewStateGraph(((Triplet) node).Third, depth+1, "Third: ");
                }
                else if (node is Pair)
                {
                    tw.Write(String.Concat(Indent(depth), label, "PAIR"));
                    ParseViewStateGraph(((Pair) node).First, depth+1, "First: ");
                    ParseViewStateGraph(((Pair) node).Second, depth+1, "Second: ");
                }
                else if (node is ArrayList)
                {
                    tw.Write(String.Concat(Indent(depth), label, "ARRAYLIST"));
    
                    // display array values
                    for (int i = 0; i < ((ArrayList) node).Count; i++)
                        ParseViewStateGraph(((ArrayList) node)[i], depth+1, String.Format("({0}) ", i));
                }
                else if (node.GetType().IsArray)
                {
                    tw.Write(String.Concat(Indent(depth), label, "ARRAY "));
                    tw.Write(String.Concat("(", node.GetType().ToString(), ")"));
                    IEnumerator e = ((Array) node).GetEnumerator();
                    int count = 0;
                    while (e.MoveNext())
                        ParseViewStateGraph(e.Current, depth+1, String.Format("({0}) ", count++));
                }
                else if (node.GetType().IsPrimitive || node is string)
                {
                    tw.Write(String.Concat(Indent(depth), label));
                    tw.Write(node.ToString() + " (" + node.GetType().ToString() + ")");
                }
                else
                {
                    tw.Write(String.Concat(Indent(depth), label, "OTHER - "));
                    tw.Write(node.GetType().ToString());
                }
            }
            #endregion
    
            /// 
            /// Returns a string containing the  property value a specified number of times.
            /// 
            /// The number of times to repeat the  property.
            /// A string containing the  property value a specified number of times.
            protected virtual string Indent(int depth)
            {
                StringBuilder sb = new StringBuilder(IndentString.Length * depth);
                for (int i = 0; i < depth; i++)
                    sb.Append(IndentString);
    
                return sb.ToString();
            }
            #endregion
    
            #region Properties
            /// 
            /// Specifies the indentation to use for each level when displaying the object graph.
            /// 
            /// A string value; the default is three blank spaces.
            public string IndentString
            {
                get
                {
                    return indentString;
                }
                set
                {
                    indentString = value;
                }
            }
            #endregion
        }
    }
    

    And here's a simple page to read the viewstate from a textbox and graph it using the above code

    private void btnParse_Click(object sender, System.EventArgs e)
            {
                // parse the viewState
                StringWriter writer = new StringWriter();
                ViewStateParser p = new ViewStateParser(writer);
    
                p.ParseViewStateGraph(txtViewState.Text);
                ltlViewState.Text = writer.ToString();
            }
    

提交回复
热议问题