<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: ViewStateViewer: A GUI Tool for deserializing/reserializing ViewState</title>
	<atom:link href="http://labs.neohapsis.com/2009/08/03/viewstateviewer-a-gui-tool-for-deserializingreserializing-viewstate/feed/" rel="self" type="application/rss+xml" />
	<link>http://labs.neohapsis.com/2009/08/03/viewstateviewer-a-gui-tool-for-deserializingreserializing-viewstate/</link>
	<description>Managing Risk and Security since 1998</description>
	<lastBuildDate>Tue, 31 Jan 2012 17:39:48 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Patrick Toomey</title>
		<link>http://labs.neohapsis.com/2009/08/03/viewstateviewer-a-gui-tool-for-deserializingreserializing-viewstate/#comment-982</link>
		<dc:creator><![CDATA[Patrick Toomey]]></dc:creator>
		<pubDate>Thu, 24 Feb 2011 17:38:00 +0000</pubDate>
		<guid isPermaLink="false">http://labs.neohapsis.com/?p=415#comment-982</guid>
		<description><![CDATA[My apologies.  We were in the middle of reorganizing the blog static file directory and this slipped through the cracks.  I think it should be updated.  Thanks for the heads up.]]></description>
		<content:encoded><![CDATA[<p>My apologies.  We were in the middle of reorganizing the blog static file directory and this slipped through the cracks.  I think it should be updated.  Thanks for the heads up.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: CR</title>
		<link>http://labs.neohapsis.com/2009/08/03/viewstateviewer-a-gui-tool-for-deserializingreserializing-viewstate/#comment-978</link>
		<dc:creator><![CDATA[CR]]></dc:creator>
		<pubDate>Tue, 22 Feb 2011 15:34:31 +0000</pubDate>
		<guid isPermaLink="false">http://labs.neohapsis.com/?p=415#comment-978</guid>
		<description><![CDATA[Sadly the Plugin seems not to be available on the site anymore. The only plugin currently available does only decode and no recode :(]]></description>
		<content:encoded><![CDATA[<p>Sadly the Plugin seems not to be available on the site anymore. The only plugin currently available does only decode and no recode <img src='http://s0.wp.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Casaba Security &#187; Watcher 1.3.0 released &#171;</title>
		<link>http://labs.neohapsis.com/2009/08/03/viewstateviewer-a-gui-tool-for-deserializingreserializing-viewstate/#comment-409</link>
		<dc:creator><![CDATA[Casaba Security &#187; Watcher 1.3.0 released &#171;]]></dc:creator>
		<pubDate>Thu, 25 Feb 2010 17:41:11 +0000</pubDate>
		<guid isPermaLink="false">http://labs.neohapsis.com/?p=415#comment-409</guid>
		<description><![CDATA[[...] [1] Trustwave advisory https://www.trustwave.com/spiderlabs/advisories/TWSL2010-001.txt [2] ViewStateViewer plugin for Fiddler http://labs.neohapsis.com/2009/08/03/viewstateviewer-a-gui-tool-for-deserializingreserializing-views... [...]]]></description>
		<content:encoded><![CDATA[<p>[...] [1] Trustwave advisory <a href="https://www.trustwave.com/spiderlabs/advisories/TWSL2010-001.txt" rel="nofollow">https://www.trustwave.com/spiderlabs/advisories/TWSL2010-001.txt</a> [2] ViewStateViewer plugin for Fiddler <a href="http://labs.neohapsis.com/2009/08/03/viewstateviewer-a-gui-tool-for-deserializingreserializing-views.." rel="nofollow">http://labs.neohapsis.com/2009/08/03/viewstateviewer-a-gui-tool-for-deserializingreserializing-views..</a>. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sacha faust</title>
		<link>http://labs.neohapsis.com/2009/08/03/viewstateviewer-a-gui-tool-for-deserializingreserializing-viewstate/#comment-340</link>
		<dc:creator><![CDATA[sacha faust]]></dc:creator>
		<pubDate>Wed, 25 Nov 2009 08:29:18 +0000</pubDate>
		<guid isPermaLink="false">http://labs.neohapsis.com/?p=415#comment-340</guid>
		<description><![CDATA[I ran into the same issue. Replace ViewState.cs with the following:

/* 
   Copyright (c) 2009, Neohapsis, Inc.
   All rights reserved.

 Implementation by Patrick Toomey

 Redistribution and use in source and binary forms, with or without modification, 
 are permitted provided that the following conditions are met: 

  - Redistributions of source code must retain the above copyright notice, this list 
    of conditions and the following disclaimer. 
  - Redistributions in binary form must reproduce the above copyright notice, this 
    list of conditions and the following disclaimer in the documentation and/or 
    other materials provided with the distribution. 
  - Neither the name of Neohapsis nor the names of its contributors may be used to 
    endorse or promote products derived from this software without specific prior 
    written permission. 

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot; AND 
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 
 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Collections.Specialized;
using System.Drawing;
using System.Web.UI;
using System.Web;
using System.Text.RegularExpressions;
using System.Reflection;
using System.ComponentModel;

namespace ViewState
{
    abstract class ViewState
    {
        protected internal int m_version = 0;           // ex. 1 (for ASP.Net 1.1)
        protected internal bool m_encrypted = false;
        protected internal bool m_MACProtected = false;
        protected internal String m_versionString = &quot;&quot;;  // ex. ASP.Net 2.0
        protected internal String m_viewStateBase64 = &quot;&quot;; // stores the  serialized after decoding the original viewstate and then reenncoding it (all MACs and such will be removed)
        protected internal String m_viewStateXML = &quot;&quot;;
        protected internal String m_MAC = &quot;None&quot;;

        public ViewState(String viewStateBase64)
        {
            m_viewStateBase64 = viewStateBase64;
            computeVersionInfo();
            computeMACInfo();
            m_viewStateXML = getViewStateXMLFromBase64(m_viewStateBase64);

        }

        public ViewState(XmlDocument viewStateXMLDocument)
        {
            m_version = Int32.Parse(viewStateXMLDocument.GetElementsByTagName(&quot;Version&quot;).Item(0).InnerText);
            m_versionString = viewStateXMLDocument.GetElementsByTagName(&quot;VersionString&quot;).Item(0).InnerText;
            m_MAC = viewStateXMLDocument.GetElementsByTagName(&quot;MAC&quot;).Item(0).InnerText;
            if (m_MAC == &quot;None&quot;)
            {
                m_MACProtected = false;
            }
            else
            {
                m_MACProtected = true;
            }
            StringBuilder sb = new StringBuilder();
            //XmlWriterSettings xws = new XmlWriterSettings();
            //xws.Indent = false;
            //XmlWriter xw = XmlWriter.Create(sb, xws); 
            StringWriter writer = new StringWriter(sb);
            viewStateXMLDocument.Save(writer);
            m_viewStateXML = sb.ToString();
            m_viewStateBase64 = getViewStateBase64FromXMLTree(viewStateXMLDocument);
            

        }

        public static byte[] toByteArray(String HexString)
        {

            int NumberChars = HexString.Length;

            byte[] bytes = new byte[NumberChars / 2];

            for (int i = 0; i &lt; NumberChars; i += 2)
            {

                bytes[i / 2] = Convert.ToByte(HexString.Substring(i, 2), 16);

            }

            return bytes;

        }
        protected abstract void computeMACInfo();
        protected abstract String getViewStateXMLFromBase64(String viewStateBase64);
        protected abstract String   getViewStateBase64FromXMLTree(XmlDocument dom);
        protected String getViewStateBase64WithMAC()
        {
            if (!m_MACProtected)
            {
                return m_viewStateBase64;
            }
            byte[] viewStateBytes = System.Convert.FromBase64String(m_viewStateBase64);
            byte[] MACBytes = ViewState.toByteArray(m_MAC);
            byte[] combinedBytes = new byte[viewStateBytes.Length + MACBytes.Length];
            viewStateBytes.CopyTo(combinedBytes, 0);
            MACBytes.CopyTo(combinedBytes, viewStateBytes.Length);
            return System.Convert.ToBase64String(combinedBytes);
        }

        protected static object buildObjectElement(XmlNode xmlNode)
        {
            String type = xmlNode.Name;
                if (type == &quot;System.Web.UI.Pair&quot;)
                {
                    Pair pair = new Pair();
                    pair.First = buildObjectElement((XmlNode)xmlNode.ChildNodes.Item(0));
                    pair.Second = buildObjectElement((XmlNode)xmlNode.ChildNodes.Item(1));
                    return pair;
                }
                else if (type == &quot;System.Web.UI.Triplet&quot;)
                {
                    Triplet triplet = new Triplet();
                    triplet.First = buildObjectElement((XmlNode)xmlNode.ChildNodes.Item(0));
                    triplet.Second = buildObjectElement((XmlNode)xmlNode.ChildNodes.Item(1));
                    triplet.Third = buildObjectElement((XmlNode)xmlNode.ChildNodes.Item(2));
                    return triplet;
                }
                else if (type == &quot;System.Collections.ArrayList&quot;)
                {
                    ArrayList arrayList = new ArrayList();
                    foreach (XmlNode innerXmlNode in xmlNode)
                    {
                        arrayList.Add(buildObjectElement(innerXmlNode));
                    }
                    return arrayList;
                }
                else if (type.StartsWith(&quot;System.Array-&quot;))
                {
                    Array array = null;
                    String arrayTypeString = type.Substring(&quot;System.Array-&quot;.Length);
                    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
                    Type arrayType = null;
                    foreach (Assembly assembly in assemblies)
                    {
                        arrayType = assembly.GetType(arrayTypeString);
                        if (arrayType != null)
                            break;
                    }

                    if (arrayType != null) 
                    {
                        array = Array.CreateInstance(arrayType,xmlNode.ChildNodes.Count);
                        for (int i = array.GetLowerBound(0); i &lt;= array.GetUpperBound(0); i++)
                        {
                            array.SetValue(buildObjectElement((XmlNode)xmlNode.ChildNodes.Item(i)),i);
                        }
                    }
                    return array;
                }
                else if (type == &quot;System.Collections.Hashtable&quot;)
                {
                    Hashtable hashTable = new Hashtable();
                    return hashTable; // this needs to be fixed...I don&#039;t know what the child elements are

                }
                else if (type == &quot;System.Collections.Specialized.HybridDictionary&quot;)
                {
                    HybridDictionary hybridDictionary = new HybridDictionary();
                    foreach (XmlNode innerXmlNode in xmlNode)
                    {
                        DictionaryEntry dictionaryEntry = (DictionaryEntry)buildObjectElement((XmlNode)innerXmlNode);
                        hybridDictionary.Add(dictionaryEntry.Key, dictionaryEntry.Value);
                    }
                    return hybridDictionary;
                }
                /*else if (type == &quot;System.Collections.IDictionary&quot;)
                {
                    IDictionary iDictionary = new IDictionary();
                    return iDictionary; // hmmm...not quite sure if we will ever see an IDicionary itself since it is an interface
                }*/

                else if (type == &quot;System.Collections.DictionaryEntry&quot;)
                {
                    DictionaryEntry dictionaryEnry = new DictionaryEntry();
                    dictionaryEnry.Key = buildObjectElement((XmlNode)xmlNode.ChildNodes.Item(0));
                    dictionaryEnry.Value = buildObjectElement((XmlNode)xmlNode.ChildNodes.Item(1));
                    return dictionaryEnry;
                }
                else if (type == &quot;System.String&quot;)
                {
                    // horrible hack because XML doesn&#039;t like to keep around whitespace.  All empty strings in our original xml get turned into 
                    // a newline followed by any number of spaces.  We&#039;ll just regex it and return the correct empty string.
                    // this occurs because we are using xml:space=preserve attributes. If we don&#039;t use attributes we have issues with 
                    // elements composed only of spaces...ugh
                    String pattern = @&quot;^\r\n +$&quot;;
                    Match match = Regex.Match(xmlNode.InnerText, pattern, RegexOptions.Multiline);
                    if (match.Success)
                    {
                        return &quot;&quot;;
                    }
                    else
                    {
                        return xmlNode.InnerText;
                    }
                }
                else if (type == &quot;System.Web.UI.IndexedString&quot;)
                {
                    return new IndexedString(xmlNode.InnerText);
                }
                else if (type == &quot;System.Int64&quot;)
                {
                    return Int64.Parse(xmlNode.InnerText);
                }
                else if (type == &quot;System.Int32&quot;)
                {
                    return Int32.Parse(xmlNode.InnerText);
                }
                else if (type == &quot;System.Int16&quot;)
                {
                    return Int16.Parse(xmlNode.InnerText);
                }
                else if (type == &quot;System.Boolean&quot;)
                {
                    if (xmlNode.InnerText == &quot;True&quot;)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else if (type == &quot;System.Drawing.Color&quot;)
                {
                    //we use the type converter instead of instantiating a new color, as most colors are not dynamic and are pulled form teh color struct
                    String colorString = xmlNode.InnerText;
#if DEBUG
                    try
                    {
                        Color color = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString(colorString);
                        return color;
                    }
                    catch (Exception colorEx)
                    {
                        return new Color();

                    }
#else
                    Color color = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString(colorString);

                    return color;
#endif

                }
                else if (type == &quot;Null&quot; &amp;&amp; xmlNode.InnerText == &quot;True&quot;)
                {
                    return null;
                }
                // we try to do a best guess for unknown types using reflection
                else
                {
                    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
                    Type realType = null;
                    foreach (Assembly assembly in assemblies)
                    {
                        realType = assembly.GetType(type);
                        if (realType != null)
                            break;
                    }
                    object[] args = new object[1];
                    args[0] = xmlNode.InnerText;

                    // Change being

                    //if (Utils.InheritFrom(realType, typeof(System.Enum)))
                    //{
                    if (realType.IsSubclassOf(typeof(System.Enum)))
                    {
                        return Enum.Parse(realType, xmlNode.InnerText);
                    }
                    else
                    {

#if DEBUG
                        try
                        {
                            return System.Activator.CreateInstance(realType, args);
                        }
                        catch (Exception ex)
                        {

                            return null;
                        }
                    }
#else
                    return System.Activator.CreateInstance(realType, args);
#endif
                }
  
        }

        public virtual String viewStateXML
        {
            get
            {
                return m_viewStateXML;
            }

        }
        public String viewStateBase64
        {
            get
            {
                return getViewStateBase64WithMAC();
            }
        }


        public String MAC
        {
            get
            {
                return m_MAC;
            }
        }

        public bool MACProected
        {
            get
            {
                return m_MACProtected;
            }
        }
        public int version
        {
            get
            {
                return m_version;
            }
        }

        public String versionString
        {
            get
            {
                return m_versionString;
            }
        }
        public bool encrypted
        {
            get
            {
                return m_encrypted;
            }
        }

        protected void computeVersionInfo()
        {
            if (m_viewStateBase64 == null)
            {
                return;
            }

            m_version = getViewStateVersionFromBase64String(m_viewStateBase64);
            switch (m_version)
            {
                case 2:
                    m_versionString = &quot;ASP.Net 2.X&quot;;
                    m_encrypted = false;
                    break;
                case 1:
                    m_versionString = &quot;ASP.Net 1.X&quot;;
                    m_encrypted = false;
                    break;
                default:
                    m_versionString = &quot;Unknown&quot;;
                    m_encrypted = true; //this isn&#039;t guranteed...but we&#039;ll assume it is true for now
                    break;
            }

            return;
        }

        public static ViewState newViewStateFromBase64String(String viewStateString)
        {
            ViewState viewState = null;
            int viewStateVersion = getViewStateVersionFromBase64String(viewStateString);
            switch (viewStateVersion)
            {
                case 1:
                    viewState = new ViewState_Dot_Net_1_0(viewStateString);
                    break;
                case 2:
                    viewState = new ViewState_Dot_Net_2_0(viewStateString);
                    break;
                default:
                    viewState = null;
                    break;

            }
            return viewState;
        }


        public static ViewState newViewStateFromXMLString(String viewStateXML)
        {
            ViewState viewState = null;
            int viewStateVersion = 0;
            String versionPattern = @&quot;(.+)&quot;;
            Match match = Regex.Match(viewStateXML, versionPattern, RegexOptions.Singleline);
            if (match.Success)
            {
                viewStateVersion = Int32.Parse(match.Groups[1].Value);
            }
            // if this is viewstate version 1 we need to patch a few things up
            if (viewStateVersion == 1)
            {
                String viewStatePattern = @&quot;(.+)&quot;;
                match = Regex.Match(viewStateXML, viewStatePattern, RegexOptions.Singleline);
                if (match.Success)
                {
                    viewStateXML = viewStateXML.Replace(match.Groups[1].Value, HttpUtility.HtmlEncode(match.Groups[1].Value));
                }

            }

            // horrible horrible hack to account for RichTextBox removing CRLF with a LF...anyone have a better idea???
            viewStateXML = viewStateXML.Replace(&quot;\n&quot;, &quot;\r\n&quot;);
            
            XmlDocument dom = new XmlDocument();
  
            StringReader reader = new StringReader(viewStateXML);
            //dom.PreserveWhitespace = true;
            dom.Load(reader);
            switch (viewStateVersion)
            {
                case 1:
                    viewState = new ViewState_Dot_Net_1_0(dom);
                    break;
                case 2:
                    viewState = new ViewState_Dot_Net_2_0(dom);
                    break;
                default:
                    viewState = null;
                    break;

            }
            return viewState;
        } 

        public static Int32 getViewStateVersionFromBase64String(String viewStateBase64)
        {
            byte[] viewStateBytes;
            try
            {
                viewStateBytes = System.Convert.FromBase64String(viewStateBase64);
            }
            catch (System.ArgumentNullException)
            {
                return 0;
            }
            if (viewStateBytes.Length &gt;= 2 &amp;&amp;
                    viewStateBytes[0] == &#039;\xff&#039; &amp;&amp;
                    viewStateBytes[1] == &#039;\x01&#039;)
            {
                return 2;
            }
            else if (viewStateBytes.Length &gt;= 2 &amp;&amp;
                         viewStateBytes[0] == &#039;t&#039; &amp;&amp;
                         viewStateBytes[1] == &#039;&lt;&#039;)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }

    }
}]]></description>
		<content:encoded><![CDATA[<p>I ran into the same issue. Replace ViewState.cs with the following:</p>
<p>/*<br />
   Copyright (c) 2009, Neohapsis, Inc.<br />
   All rights reserved.</p>
<p> Implementation by Patrick Toomey</p>
<p> Redistribution and use in source and binary forms, with or without modification,<br />
 are permitted provided that the following conditions are met: </p>
<p>  &#8211; Redistributions of source code must retain the above copyright notice, this list<br />
    of conditions and the following disclaimer.<br />
  &#8211; Redistributions in binary form must reproduce the above copyright notice, this<br />
    list of conditions and the following disclaimer in the documentation and/or<br />
    other materials provided with the distribution.<br />
  &#8211; Neither the name of Neohapsis nor the names of its contributors may be used to<br />
    endorse or promote products derived from this software without specific prior<br />
    written permission. </p>
<p> THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &#8220;AS IS&#8221; AND<br />
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED<br />
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE<br />
 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR<br />
 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES<br />
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;<br />
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON<br />
 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT<br />
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS<br />
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<br />
*/<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Collections;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Xml;<br />
using System.IO;<br />
using System.Collections.Specialized;<br />
using System.Drawing;<br />
using System.Web.UI;<br />
using System.Web;<br />
using System.Text.RegularExpressions;<br />
using System.Reflection;<br />
using System.ComponentModel;</p>
<p>namespace ViewState<br />
{<br />
    abstract class ViewState<br />
    {<br />
        protected internal int m_version = 0;           // ex. 1 (for ASP.Net 1.1)<br />
        protected internal bool m_encrypted = false;<br />
        protected internal bool m_MACProtected = false;<br />
        protected internal String m_versionString = &#8220;&#8221;;  // ex. ASP.Net 2.0<br />
        protected internal String m_viewStateBase64 = &#8220;&#8221;; // stores the  serialized after decoding the original viewstate and then reenncoding it (all MACs and such will be removed)<br />
        protected internal String m_viewStateXML = &#8220;&#8221;;<br />
        protected internal String m_MAC = &#8220;None&#8221;;</p>
<p>        public ViewState(String viewStateBase64)<br />
        {<br />
            m_viewStateBase64 = viewStateBase64;<br />
            computeVersionInfo();<br />
            computeMACInfo();<br />
            m_viewStateXML = getViewStateXMLFromBase64(m_viewStateBase64);</p>
<p>        }</p>
<p>        public ViewState(XmlDocument viewStateXMLDocument)<br />
        {<br />
            m_version = Int32.Parse(viewStateXMLDocument.GetElementsByTagName(&#8220;Version&#8221;).Item(0).InnerText);<br />
            m_versionString = viewStateXMLDocument.GetElementsByTagName(&#8220;VersionString&#8221;).Item(0).InnerText;<br />
            m_MAC = viewStateXMLDocument.GetElementsByTagName(&#8220;MAC&#8221;).Item(0).InnerText;<br />
            if (m_MAC == &#8220;None&#8221;)<br />
            {<br />
                m_MACProtected = false;<br />
            }<br />
            else<br />
            {<br />
                m_MACProtected = true;<br />
            }<br />
            StringBuilder sb = new StringBuilder();<br />
            //XmlWriterSettings xws = new XmlWriterSettings();<br />
            //xws.Indent = false;<br />
            //XmlWriter xw = XmlWriter.Create(sb, xws);<br />
            StringWriter writer = new StringWriter(sb);<br />
            viewStateXMLDocument.Save(writer);<br />
            m_viewStateXML = sb.ToString();<br />
            m_viewStateBase64 = getViewStateBase64FromXMLTree(viewStateXMLDocument);</p>
<p>        }</p>
<p>        public static byte[] toByteArray(String HexString)<br />
        {</p>
<p>            int NumberChars = HexString.Length;</p>
<p>            byte[] bytes = new byte[NumberChars / 2];</p>
<p>            for (int i = 0; i &lt; NumberChars; i += 2)<br />
            {</p>
<p>                bytes[i / 2] = Convert.ToByte(HexString.Substring(i, 2), 16);</p>
<p>            }</p>
<p>            return bytes;</p>
<p>        }<br />
        protected abstract void computeMACInfo();<br />
        protected abstract String getViewStateXMLFromBase64(String viewStateBase64);<br />
        protected abstract String   getViewStateBase64FromXMLTree(XmlDocument dom);<br />
        protected String getViewStateBase64WithMAC()<br />
        {<br />
            if (!m_MACProtected)<br />
            {<br />
                return m_viewStateBase64;<br />
            }<br />
            byte[] viewStateBytes = System.Convert.FromBase64String(m_viewStateBase64);<br />
            byte[] MACBytes = ViewState.toByteArray(m_MAC);<br />
            byte[] combinedBytes = new byte[viewStateBytes.Length + MACBytes.Length];<br />
            viewStateBytes.CopyTo(combinedBytes, 0);<br />
            MACBytes.CopyTo(combinedBytes, viewStateBytes.Length);<br />
            return System.Convert.ToBase64String(combinedBytes);<br />
        }</p>
<p>        protected static object buildObjectElement(XmlNode xmlNode)<br />
        {<br />
            String type = xmlNode.Name;<br />
                if (type == &quot;System.Web.UI.Pair&quot;)<br />
                {<br />
                    Pair pair = new Pair();<br />
                    pair.First = buildObjectElement((XmlNode)xmlNode.ChildNodes.Item(0));<br />
                    pair.Second = buildObjectElement((XmlNode)xmlNode.ChildNodes.Item(1));<br />
                    return pair;<br />
                }<br />
                else if (type == &quot;System.Web.UI.Triplet&quot;)<br />
                {<br />
                    Triplet triplet = new Triplet();<br />
                    triplet.First = buildObjectElement((XmlNode)xmlNode.ChildNodes.Item(0));<br />
                    triplet.Second = buildObjectElement((XmlNode)xmlNode.ChildNodes.Item(1));<br />
                    triplet.Third = buildObjectElement((XmlNode)xmlNode.ChildNodes.Item(2));<br />
                    return triplet;<br />
                }<br />
                else if (type == &quot;System.Collections.ArrayList&quot;)<br />
                {<br />
                    ArrayList arrayList = new ArrayList();<br />
                    foreach (XmlNode innerXmlNode in xmlNode)<br />
                    {<br />
                        arrayList.Add(buildObjectElement(innerXmlNode));<br />
                    }<br />
                    return arrayList;<br />
                }<br />
                else if (type.StartsWith(&quot;System.Array-&quot;))<br />
                {<br />
                    Array array = null;<br />
                    String arrayTypeString = type.Substring(&quot;System.Array-&quot;.Length);<br />
                    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();<br />
                    Type arrayType = null;<br />
                    foreach (Assembly assembly in assemblies)<br />
                    {<br />
                        arrayType = assembly.GetType(arrayTypeString);<br />
                        if (arrayType != null)<br />
                            break;<br />
                    }</p>
<p>                    if (arrayType != null)<br />
                    {<br />
                        array = Array.CreateInstance(arrayType,xmlNode.ChildNodes.Count);<br />
                        for (int i = array.GetLowerBound(0); i &lt;= array.GetUpperBound(0); i++)<br />
                        {<br />
                            array.SetValue(buildObjectElement((XmlNode)xmlNode.ChildNodes.Item(i)),i);<br />
                        }<br />
                    }<br />
                    return array;<br />
                }<br />
                else if (type == &quot;System.Collections.Hashtable&quot;)<br />
                {<br />
                    Hashtable hashTable = new Hashtable();<br />
                    return hashTable; // this needs to be fixed&#8230;I don&#039;t know what the child elements are</p>
<p>                }<br />
                else if (type == &quot;System.Collections.Specialized.HybridDictionary&quot;)<br />
                {<br />
                    HybridDictionary hybridDictionary = new HybridDictionary();<br />
                    foreach (XmlNode innerXmlNode in xmlNode)<br />
                    {<br />
                        DictionaryEntry dictionaryEntry = (DictionaryEntry)buildObjectElement((XmlNode)innerXmlNode);<br />
                        hybridDictionary.Add(dictionaryEntry.Key, dictionaryEntry.Value);<br />
                    }<br />
                    return hybridDictionary;<br />
                }<br />
                /*else if (type == &quot;System.Collections.IDictionary&quot;)<br />
                {<br />
                    IDictionary iDictionary = new IDictionary();<br />
                    return iDictionary; // hmmm&#8230;not quite sure if we will ever see an IDicionary itself since it is an interface<br />
                }*/</p>
<p>                else if (type == &quot;System.Collections.DictionaryEntry&quot;)<br />
                {<br />
                    DictionaryEntry dictionaryEnry = new DictionaryEntry();<br />
                    dictionaryEnry.Key = buildObjectElement((XmlNode)xmlNode.ChildNodes.Item(0));<br />
                    dictionaryEnry.Value = buildObjectElement((XmlNode)xmlNode.ChildNodes.Item(1));<br />
                    return dictionaryEnry;<br />
                }<br />
                else if (type == &quot;System.String&quot;)<br />
                {<br />
                    // horrible hack because XML doesn&#039;t like to keep around whitespace.  All empty strings in our original xml get turned into<br />
                    // a newline followed by any number of spaces.  We&#039;ll just regex it and return the correct empty string.<br />
                    // this occurs because we are using xml:space=preserve attributes. If we don&#039;t use attributes we have issues with<br />
                    // elements composed only of spaces&#8230;ugh<br />
                    String pattern = @&quot;^\r\n +$&quot;;<br />
                    Match match = Regex.Match(xmlNode.InnerText, pattern, RegexOptions.Multiline);<br />
                    if (match.Success)<br />
                    {<br />
                        return &quot;&quot;;<br />
                    }<br />
                    else<br />
                    {<br />
                        return xmlNode.InnerText;<br />
                    }<br />
                }<br />
                else if (type == &quot;System.Web.UI.IndexedString&quot;)<br />
                {<br />
                    return new IndexedString(xmlNode.InnerText);<br />
                }<br />
                else if (type == &quot;System.Int64&quot;)<br />
                {<br />
                    return Int64.Parse(xmlNode.InnerText);<br />
                }<br />
                else if (type == &quot;System.Int32&quot;)<br />
                {<br />
                    return Int32.Parse(xmlNode.InnerText);<br />
                }<br />
                else if (type == &quot;System.Int16&quot;)<br />
                {<br />
                    return Int16.Parse(xmlNode.InnerText);<br />
                }<br />
                else if (type == &quot;System.Boolean&quot;)<br />
                {<br />
                    if (xmlNode.InnerText == &quot;True&quot;)<br />
                    {<br />
                        return true;<br />
                    }<br />
                    else<br />
                    {<br />
                        return false;<br />
                    }<br />
                }<br />
                else if (type == &quot;System.Drawing.Color&quot;)<br />
                {<br />
                    //we use the type converter instead of instantiating a new color, as most colors are not dynamic and are pulled form teh color struct<br />
                    String colorString = xmlNode.InnerText;<br />
#if DEBUG<br />
                    try<br />
                    {<br />
                        Color color = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString(colorString);<br />
                        return color;<br />
                    }<br />
                    catch (Exception colorEx)<br />
                    {<br />
                        return new Color();</p>
<p>                    }<br />
#else<br />
                    Color color = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString(colorString);</p>
<p>                    return color;<br />
#endif</p>
<p>                }<br />
                else if (type == &quot;Null&quot; &amp;&amp; xmlNode.InnerText == &quot;True&quot;)<br />
                {<br />
                    return null;<br />
                }<br />
                // we try to do a best guess for unknown types using reflection<br />
                else<br />
                {<br />
                    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();<br />
                    Type realType = null;<br />
                    foreach (Assembly assembly in assemblies)<br />
                    {<br />
                        realType = assembly.GetType(type);<br />
                        if (realType != null)<br />
                            break;<br />
                    }<br />
                    object[] args = new object[1];<br />
                    args[0] = xmlNode.InnerText;</p>
<p>                    // Change being</p>
<p>                    //if (Utils.InheritFrom(realType, typeof(System.Enum)))<br />
                    //{<br />
                    if (realType.IsSubclassOf(typeof(System.Enum)))<br />
                    {<br />
                        return Enum.Parse(realType, xmlNode.InnerText);<br />
                    }<br />
                    else<br />
                    {</p>
<p>#if DEBUG<br />
                        try<br />
                        {<br />
                            return System.Activator.CreateInstance(realType, args);<br />
                        }<br />
                        catch (Exception ex)<br />
                        {</p>
<p>                            return null;<br />
                        }<br />
                    }<br />
#else<br />
                    return System.Activator.CreateInstance(realType, args);<br />
#endif<br />
                }</p>
<p>        }</p>
<p>        public virtual String viewStateXML<br />
        {<br />
            get<br />
            {<br />
                return m_viewStateXML;<br />
            }</p>
<p>        }<br />
        public String viewStateBase64<br />
        {<br />
            get<br />
            {<br />
                return getViewStateBase64WithMAC();<br />
            }<br />
        }</p>
<p>        public String MAC<br />
        {<br />
            get<br />
            {<br />
                return m_MAC;<br />
            }<br />
        }</p>
<p>        public bool MACProected<br />
        {<br />
            get<br />
            {<br />
                return m_MACProtected;<br />
            }<br />
        }<br />
        public int version<br />
        {<br />
            get<br />
            {<br />
                return m_version;<br />
            }<br />
        }</p>
<p>        public String versionString<br />
        {<br />
            get<br />
            {<br />
                return m_versionString;<br />
            }<br />
        }<br />
        public bool encrypted<br />
        {<br />
            get<br />
            {<br />
                return m_encrypted;<br />
            }<br />
        }</p>
<p>        protected void computeVersionInfo()<br />
        {<br />
            if (m_viewStateBase64 == null)<br />
            {<br />
                return;<br />
            }</p>
<p>            m_version = getViewStateVersionFromBase64String(m_viewStateBase64);<br />
            switch (m_version)<br />
            {<br />
                case 2:<br />
                    m_versionString = &quot;ASP.Net 2.X&quot;;<br />
                    m_encrypted = false;<br />
                    break;<br />
                case 1:<br />
                    m_versionString = &quot;ASP.Net 1.X&quot;;<br />
                    m_encrypted = false;<br />
                    break;<br />
                default:<br />
                    m_versionString = &quot;Unknown&quot;;<br />
                    m_encrypted = true; //this isn&#039;t guranteed&#8230;but we&#039;ll assume it is true for now<br />
                    break;<br />
            }</p>
<p>            return;<br />
        }</p>
<p>        public static ViewState newViewStateFromBase64String(String viewStateString)<br />
        {<br />
            ViewState viewState = null;<br />
            int viewStateVersion = getViewStateVersionFromBase64String(viewStateString);<br />
            switch (viewStateVersion)<br />
            {<br />
                case 1:<br />
                    viewState = new ViewState_Dot_Net_1_0(viewStateString);<br />
                    break;<br />
                case 2:<br />
                    viewState = new ViewState_Dot_Net_2_0(viewStateString);<br />
                    break;<br />
                default:<br />
                    viewState = null;<br />
                    break;</p>
<p>            }<br />
            return viewState;<br />
        }</p>
<p>        public static ViewState newViewStateFromXMLString(String viewStateXML)<br />
        {<br />
            ViewState viewState = null;<br />
            int viewStateVersion = 0;<br />
            String versionPattern = @&quot;(.+)&#8221;;<br />
            Match match = Regex.Match(viewStateXML, versionPattern, RegexOptions.Singleline);<br />
            if (match.Success)<br />
            {<br />
                viewStateVersion = Int32.Parse(match.Groups[1].Value);<br />
            }<br />
            // if this is viewstate version 1 we need to patch a few things up<br />
            if (viewStateVersion == 1)<br />
            {<br />
                String viewStatePattern = @&#8221;(.+)&#8221;;<br />
                match = Regex.Match(viewStateXML, viewStatePattern, RegexOptions.Singleline);<br />
                if (match.Success)<br />
                {<br />
                    viewStateXML = viewStateXML.Replace(match.Groups[1].Value, HttpUtility.HtmlEncode(match.Groups[1].Value));<br />
                }</p>
<p>            }</p>
<p>            // horrible horrible hack to account for RichTextBox removing CRLF with a LF&#8230;anyone have a better idea???<br />
            viewStateXML = viewStateXML.Replace(&#8220;\n&#8221;, &#8220;\r\n&#8221;);</p>
<p>            XmlDocument dom = new XmlDocument();</p>
<p>            StringReader reader = new StringReader(viewStateXML);<br />
            //dom.PreserveWhitespace = true;<br />
            dom.Load(reader);<br />
            switch (viewStateVersion)<br />
            {<br />
                case 1:<br />
                    viewState = new ViewState_Dot_Net_1_0(dom);<br />
                    break;<br />
                case 2:<br />
                    viewState = new ViewState_Dot_Net_2_0(dom);<br />
                    break;<br />
                default:<br />
                    viewState = null;<br />
                    break;</p>
<p>            }<br />
            return viewState;<br />
        } </p>
<p>        public static Int32 getViewStateVersionFromBase64String(String viewStateBase64)<br />
        {<br />
            byte[] viewStateBytes;<br />
            try<br />
            {<br />
                viewStateBytes = System.Convert.FromBase64String(viewStateBase64);<br />
            }<br />
            catch (System.ArgumentNullException)<br />
            {<br />
                return 0;<br />
            }<br />
            if (viewStateBytes.Length &gt;= 2 &amp;&amp;<br />
                    viewStateBytes[0] == &#8216;\xff&#8217; &amp;&amp;<br />
                    viewStateBytes[1] == &#8216;\x01&#8242;)<br />
            {<br />
                return 2;<br />
            }<br />
            else if (viewStateBytes.Length &gt;= 2 &amp;&amp;<br />
                         viewStateBytes[0] == &#8216;t&#8217; &amp;&amp;<br />
                         viewStateBytes[1] == &#8216;&lt;&#039;)<br />
            {<br />
                return 1;<br />
            }<br />
            else<br />
            {<br />
                return 0;<br />
            }<br />
        }</p>
<p>    }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sauby</title>
		<link>http://labs.neohapsis.com/2009/08/03/viewstateviewer-a-gui-tool-for-deserializingreserializing-viewstate/#comment-307</link>
		<dc:creator><![CDATA[Sauby]]></dc:creator>
		<pubDate>Thu, 27 Aug 2009 13:30:52 +0000</pubDate>
		<guid isPermaLink="false">http://labs.neohapsis.com/?p=415#comment-307</guid>
		<description><![CDATA[Hey,

Nice work.

I am facing some issues with this plugin. It smoothly deserializes the viewstate but when i try to r-serialize it, even without making a change, it throws an error: &quot;cannot encode malformed xml&quot;. This is regardless of making any changes to the xml in lower half of plugin window.

Kindly help.

/sauby]]></description>
		<content:encoded><![CDATA[<p>Hey,</p>
<p>Nice work.</p>
<p>I am facing some issues with this plugin. It smoothly deserializes the viewstate but when i try to r-serialize it, even without making a change, it throws an error: &#8220;cannot encode malformed xml&#8221;. This is regardless of making any changes to the xml in lower half of plugin window.</p>
<p>Kindly help.</p>
<p>/sauby</p>
]]></content:encoded>
	</item>
</channel>
</rss>

