Click here to Skip to main content
15,897,187 members
Articles / Web Development / HTML

Query string encryption for ASP.NET

Rate me:
Please Sign up or sign in to vote.
3.90/5 (29 votes)
15 Nov 2014CPOL2 min read 249K   4.2K   103  
Clear text query strings are a potential security threat for your web application. Thus, query strings should always be encrypted.
using System;
using System.Collections.Specialized;
using System.Text;
using System.Web.Configuration;
using System.Web.UI;
using SmartSoft.QueryStringEncryption;

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PrepareSendButton();
        }

        StringBuilder transmittedQueryStrings = new StringBuilder();

        // Parse query strings if there are any
        foreach (string key in Request.QueryString.AllKeys)
        {
            if (transmittedQueryStrings.Length > 0) transmittedQueryStrings.Append("&");
            transmittedQueryStrings.Append(string.Format("{0}={1}", key, Request.QueryString[key]));
        }

        // Output query string
        if (transmittedQueryStrings.Length > 0)
        {
            lblParam.Text = transmittedQueryStrings.ToString();
            divReceivedParam.Visible = true;
        }
    }

    private void PrepareSendButton()
    {
        NameValueCollection queryStrings = new NameValueCollection();
        queryStrings.Add("param1", "Test1");
        queryStrings.Add("param2", "Test2");
        queryStrings.Add("param3", "Test3");

        // Encrypt query strings
        string encryptedString = CryptoQueryStringHandler.EncryptQueryStrings(queryStrings,
                                        WebConfigurationManager.AppSettings["CryptoKey"]);

        btnSendParams.PostBackUrl = string.Concat("~/Default.aspx?", encryptedString);
    }
}

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
Architect Swissworx
Australia Australia
MCAD, MCPD Web Developer 2.0, MCPD Enterprise Developer 3.5

My company: Swissworx
My blog: Sitecore Experts

Hopp Schwiiz Smile | :)

Comments and Discussions