How to use encrypted URL queries in ASP.NET in C#

The article How to hide URL query information in ASP.NET in C# describes a class named EncryptedQueryString which is used in the next example to illustrate how you can use it in your projects. You have to build two web pages:

– The first one (QueryStringSender) can contain a text box for entering information and a button which is used to send the encrypted query string to the receiving page.

– The second one (QueryStringReceiver) deserializes the query string passed in through the data query string parameter

 

 

QueryStringSender

 

<form id=”Page 1″ runat=”server”>

<div>

Enter some data here: <asp:TextBox runat=”server” ID=”MyData” />

<br />

<br />

<asp:Button ID=”SendCommand” runat=”server” Text=”Send Info” OnClick=”SendCommand_Click” />

</div>

</form>

 

When the user clicks the SendCommand button, the page sends the encrypted query string to the receiving page, as follows:

 

protected void SendCommand_Click(object sender, EventArgs e)

{

EncryptedQueryString QueryString = new EncryptedQueryString();

QueryString.Add(“MyData”, MyData.Text);

QueryString.Add(“MyTime”, DateTime.Now.ToLongTimeString());

QueryString.Add(“MyDate”, DateTime.Now.ToLongDateString());

Response.Redirect(“QueryStringReceiver.aspx?data=” + QueryString.ToString());

}

QueryStringReceiver

<form id=”Page 2″ runat=”server”>

<div>

<h2>Information retrieved:</h2>

<asp:Label runat=”server” ID=”QueryStringLabel” />

</div>

</form>

 

protected void Page_Load(object sender, EventArgs e)

{

// Deserialize the encrypted query string

EncryptedQueryString QueryString = new EncryptedQueryString(Request.QueryString[“data”]);

 

// Write information to the screen

StringBuilder Info = new StringBuilder();

foreach (String key in QueryString.Keys)

{

Info.AppendFormat(“{0} = {1}<br>”, key, QueryString[key]);

}

QueryStringLabel.Text = Info.ToString();

}