Click here to Skip to main content
15,884,938 members
Articles / Web Development / ASP.NET

Post Serialized Objects (Data) in ASP.NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
25 Feb 2013CPOL2 min read 23K   5  
This is helper class to post serialized objects (Data) to other page in asp.net.
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;
using TripleDESEncryption;
namespace WebUtilities
{
	public partial class PostDataHelper
	{		
		private static string SecretKey;
		private static string InitializeVector;
		private static TrippleDESCryptoService des;
		private PostDataHelper.PostData _PostData = new PostDataHelper.PostData();
		private HttpResponse Response;
		private HttpRequest Request;
        /// <summary>
        /// Data
        /// </summary>
		public PostDataHelper.PostData Data
		{
			get
			{
				return this._PostData;
			}
		}
        /// <summary>
        /// Get/Set FormName for data
        /// </summary>
		public string FormName
		{
			get
			{
				return this.Data.FormName;
			}
			set
			{
				this.Data.FormName = value;
			}
		}
        /// <summary>
        /// Static ctor to initialize encryptor
        /// </summary>
		static PostDataHelper()
		{
			PostDataHelper.SecretKey = ConfigurationManager.AppSettings["TripleDESSecretKey"];
			PostDataHelper.InitializeVector = ConfigurationManager.AppSettings["TripleDESIntialzationVector"];			
			PostDataHelper.des = new TrippleDESCryptoService(PostDataHelper.SecretKey, PostDataHelper.InitializeVector);
		}
        /// <summary>
        /// ctor    
        /// </summary>
        /// <param name="request">Current Request</param>
        /// <param name="response">Current Response</param>
		public PostDataHelper(HttpRequest request, HttpResponse response)
		{
			this.Request = request;
			this.Response = response;
		}
        /// <summary>
        /// Reads data posted using same utility
        /// </summary>
		public void ReadPostedData()
		{
			this._PostData = new PostDataHelper.PostData();
			if (this.Request.Form != null && this.Request.Form["__TransferData"] != null)
			{
				foreach (string key in this.Request.Form.Keys)
				{
					this.Data.SetRawData(key, PostDataHelper.des.DecryptData(HttpUtility.HtmlDecode(this.Request.Form[key])));
				}
			}
		}
        /// <summary>
        /// Posts data
        /// </summary>
        /// <param name="Destination"></param>
		public void RedirectWithData(string Destination)
		{
			this.Response.Clear();
			StringBuilder sb = new StringBuilder();
			sb.Append("<html>");
			sb.Append("<body onload='document.forms[\"form\"].submit()'>");
			sb.AppendFormat("<form name='form' action='{0}' method='post'>", Destination);
			foreach (string key in this.Data)
			{
				sb.AppendFormat("<input type='hidden' name='{0}' value='{1}' />", key, HttpUtility.HtmlEncode(PostDataHelper.des.EncryptData(this.Data.GetRawData(key))));
			}
            sb.Append("<noscript>Javascript is disabled, Click submit to proceed.");
			sb.Append("<br/><input type='submit' value='submit'/>");
			sb.Append("</noscript>");
			sb.Append("</form>");
			sb.Append("</body>");
			sb.Append("</html>");
			this.Response.Write(sb.ToString());
			this.Response.End();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer TCS
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions