Visit Twellow.com

ASP.NET: HttpModule for Query String Encryption

URL parameters or query strings are often used to carry information that can be used by hackers to do identity theft or other unpleasant things.

Consider the URL example.com/?user=123&account=456 and then imaging what a hacker could do with it. Security or not, sometimes you just don't want the visitors to see all the query strings for whatever reason.

In those cases it would be nice if we could encrypt the entire query string so it wouldn't carry any readable information. The problem with one big encrypted query string is that we would break all the code that referenced the query. Code like Request.QueryString["user"] would no longer work, but as usual ASP.NET has the answer to that problem.

What we need is an HttpModule that can turn the encrypted query string into a normal readable one, so that we can still use our old logic like Request.QueryString["user"]. In other words, we want the user to see this

?enc=VXzal017xHwKKPolDWQJoLACDqQ0fE//wGkgvRTdG/GgXIBDd1

while your code sees this

?user=123&account=456.

The HttpModule

The module we need for this task must be able to do a few simple things. It must be able to encrypt the regular query string so that all your current links will automatically be encrypted. It must also be able to decrypt it again so that you can write the code as you normally would. It must also provide a method for encrypting a regular query string if you don't want to use automatic encryption.

The most important feature of the module is to make it totally plug 'n play. You should be able to apply the module to any existing website and automatically have query string encryption and decryption without changing any of your code.

Implementation

Download the QueryStringModule.cs below and put it in the App_Code folder of your website. Then add the following lines to the web.config's section:

<httpModules>

   <add type="QueryStringModule" name="QueryStringModule"/>

</httpModules>

Because automatic encryption is not always desirable the module has a comment that tells you how to turn it off. The module is well commented and should be easy to modify for any ASP.NET developer.

Example

You can encrypt query strings by using the Encrypt() method of the module from any web page or user control.

string query = QueryStringModule.Encrypt("user=123&account=456");

Then just add the encrypted query string to the links that need encryption. You don't need to use the method if you use automatic encryption.

Download

QueryStringModule.zip (1,55 KB)

Comments

Tag:

Reddit | Furl

Bookmark WebProNews:

Mads Kristensen currently works as a Senior Developer at Traceworks located
in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/

Digg This! StumbleUpon This!
AddThis Social Bookmark Widget

About the author:
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/

Comments

Not working in Server

i have download the class file and added to my project (app_code).It's working fine in my production server, but after publishing the code not working.kindly help me what the proplem?

Advance thanks and regards

Titus

your download does not work

Please chk the QueryStringModule.zip (1,55 KB) download

Download does not work

ASP.NET: HttpModule For Query String Encryption

Download does not work

ASP.NET: HttpModule For Query String Encryption

ASP.NET: HttpModule For Query String Encryption

Download for this does not work

Your download no longer

Your download no longer works! Please check.

Call back page problem

I have an external call back page that inherits the encrypted query string of parent page, then appends other parameters. Here's a sample below

Query string: "?enc=bUcbHma24aztJXuz2jAcmf2FbGhPYD0jSasZNT82z3M=&rcbID=ctl00_mainContent_RadComboBox1&rcbServerID=RadComboBox1&text=gomez&comboText=gomez&comboValue=&skin=WebBlue&external=true&timeStamp=1174595521521"

The encrypt function dies on "invalid format"

Is there a way around this, or should I just extract out the string "enc=......." during encrypt?

thx - peter

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
1 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
Subscribe to WebProNews


Send me relevant info