reCAPTCHA : Free Captcha service for ASP.NET

Its a common requirement that we require a captcha to be integrated on our page. I would like to mention a free captcha service which can be implemented in integrated on websites.

What is reCaptcha?

  1. reCAPTCHA is a free CAPTCHA service that helps to digitize books, newspapers and old time radio shows.
  2. It’s Free! Yep, reCAPTCHA is free.
  3. It’s Easy. reCAPTCHA is a Web service. As such, adopting it is as simple as adding
    a few lines of code on your site.
  4. It’s Accessible. reCAPTCHA has an audio test that allows blind people to freely
    navigate your site.
  5. It’s Secure. Most other CAPTCHA implementations can be easily broken.
  6. It’s Popular. Over 100,000 sites use reCAPTCHA, including household names like Facebook, Ticketmaster, and Craigslist.
  7. Whenever uses input data in reCaptcha control, they actually help digitizing books.

Moreover is very easy to integrate reCaptcha in our websites. Below are the steps
which are required to integrate it into a ASP.NET page.

Steps to Integrate reCaptcha in ASP.NET

  1. Register for a reCaptcha key : As a first step we need to register for recaptcha keys. Navigate to Get reCaptcha URL to signup for the keys. After we register for the keys, we get a public and private keys which we need to use in our asp.net page. By default all keys work on localhost as well.
  2. Download reCaptcha library for ASP.NET: Download the dll file from here. Also add the reference to the dll in the asp.net project.
  3. Add reCaptcha widget on ASP.NET page : Insert the reCAPTCHA control into the form you wish to protect by adding the following code snippets:
    • At the top of the aspx page, insert this:
      <%@ register
                      tagprefix="recaptcha" namespace="Recaptcha" assembly="Recaptcha" %>
    • Then insert the reCAPTCHA control inside of the form tag:
                      <recaptcha:recaptchacontrol id="recaptcha" runat="server" publickey="your_public_key"
                          privatekey="your_private_key" />
                      
  4. Make sure you use ASP.NET validation to validate your form (you should check Page.IsValid on submission).

As an example I created a ASP.NET page whose markup and code behind code looks as given below:

Markup:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RecaptchaPage.aspx.cs" Inherits="ContosoUniversity.RecaptchaPage" %>
<%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:Label Visible="true" ID="lblResult" runat="server" />

     <recaptcha:RecaptchaControl
              ID="recaptcha"
              runat="server"
              Theme="red"
              PublicKey="6LcoxcASAAAAAGAQQz_xOTk4-ALrRQri_Cf8AuhL"
              PrivateKey="6LcoxcASAAAAALfZhquqene7_4bTrzmuqHBrkuk0"
              />

          <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />

    </div>
    </form>
</body>
</html>

Code-behind

    public partial class RecaptchaPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid) 
            {
              lblResult.Text = "Captcha sucessfull!";
              lblResult.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
              lblResult.Text = "Incorrect";
              lblResult.ForeColor = System.Drawing.Color.Red;
            }
        }
    }

When I entered correct captcha text and pressed submit button following was the output:

When I entered incorrect captcha text and pressed submit button following was the output:

kick it on DotNetKicks.com

Shout it

Introduction to JSON (Javascript Object Notation)

What is JSON?
JSON (Javascript Object Notation) is a light weight data interchange format. JSON is a subset of the literal object notation in JavaScript. It can be compared to XML but parsing of JSON is very much easier than XML data. It is a text format of data and is programming language independent. In fact it very much looks like unnamed array.

Example of data in JSON
Shown below is a object declared in JSON format.

var individual = {
     "firstName": "Anky",
     "lastName": "Singh",
     "address": {
                       "streetAddress": "4th Avenue",
                       "city": "Moorpark",
                       "state": "CA",
                       "postalCode": 34523
                    },
     "phoneNumbers": [
                                 "212 555-1234",
                                 "646 555-4567"
                             ]
 };

alert("The name of Individual is " + individual.firstName + " " +  individual.lastName);
alert("The Street Address is " + individual.address.streetAddress);
alert("The Postal code is " + individual.address.postalCode);
alert("First Phone Number  is " + individual.phoneNumbers[0]);
alert("Second Phone Number  is " + individual.phoneNumbers[1]);

<em>Output would be alert windows with following messages</em>:
The name of Individual is Anky Singh
The Street Address is 4th Avenue
The Postal code is 34523
First Phone Number  is 212 555-1234
Second Phone Number  is 646 555-4567

Above code shows how an Individual object is declared using JSON. Alert statements provide an example how the properties of the object can be accessed in the same way as we do in C# or Java.

Being subset of literal notation JSON standard has got stricter rules. See www.json.org or RFC 4627 for a more formal description of the standard.

But all said simplicity of JSON data is the specialty of the standard.

kick it on DotNetKicks.com

Storing URL Re-Write rules in a seperate file

When using rewrite maps in IIS URL Rewrite it is very common to have a very large number of entries in a rewrite rules. In order to avoid cluttering the configuration file – web.config – with this configuration data the rewrite rules can be defined in a separate configuration file. That file can then be referenced from the web.config file.

For example below are few of the rules defined in one of my web application:

<system.webServer>
      <rewrite>
            <rules>
                 <rule name="HomeURL" stopProcessing="true">
                    <match url="^Home$" />
                    <conditions logicalGrouping="MatchAll"
                      trackAllCaptures="false" />
                     <action type="Rewrite" url="Pages/HomePage.aspx"
                      appendQueryString="false" logRewrittenUrl="true" />
                  </rule>
                  <rule name="DefaultURL" stopProcessing="true">
                     <match url="default" />
                     <conditions logicalGrouping="MatchAll"
                        trackAllCaptures="false" />
                     <action type="Rewrite" url="Pages/HomePage.aspx"
                        appendQueryString="false" logRewrittenUrl="true" />
                  </rule>
               </rules>            
      </rewrite>
  </system.webServer>

Create a seperate file called myRules.config in the same directory where web.config file is and move the rules to the new file:

		<rules>
                       <rule name="HomeURL" stopProcessing="true">
                             <match url="^Home$" />
                             <conditions logicalGrouping="MatchAll"
                                  trackAllCaptures="false" />
                             <action type="Rewrite" url="Pages/HomePage.aspx"
                                  appendQueryString="false" logRewrittenUrl="true" />
                        </rule>
                        <rule name="DefaultURL" stopProcessing="true">
                             <match url="default" />
                             <conditions logicalGrouping="MatchAll"
                                 trackAllCaptures="false" />
                             <action type="Rewrite" url="Pages/HomePage.aspx"
                                 appendQueryString="false" logRewrittenUrl="true" />
                        </rule>
               </rules>    

Save the new file and now open web.config file in notepad. In web.config file add the following inside the <rewrite> section:   

<system.webServer>
      <rewrite>
         <rules configSource="myRewriteMaps.config" />
      </rewrite>
 </system.webServer>

The configSource attribute tells IIS configuration that the <rewriteMaps> section is defined in a separate file myRules.config. This referenced section can be now uses as if it was defined in the same web.config file. Also, the IIS Manager UI will work well with this referenced file: when you modify or add entries to the rewrite map they will be stored in the myRules.config file.

The same approach can be used for storing rewrite mappings in seperate configuration file. For Ex:

<system.webServer>
      <rewrite>
          <rules configSource="myRewriteMaps.config" />

</rewrite>

</system.webServer>

Hope this helps!!! 🙂