Posts Tagged ‘Web’

Using Asirra in ASP.Net MVC

Tuesday, January 13th, 2009

A simple helper to integrate and validate Asirra in ASP.Net MVC.

People use CAPTCHA (Completely Automated Public Turing test to tell Computers and Humans Apart) to prevent their websites from automatic posting. Asirra is such a technique which can be used to protect websites from automatic posting.

What is Asirra

Asirra is a human interactive proof that asks users to identify photos of cats and dogs. It’s powered by over three million photos from Petfinder.com.

Asirra (Animal Species Image Recognition for Restricting Access) is a HIP that works by asking users to identify photographs of cats and dogs. This task is difficult for computers, but people can accomplish it quickly and accurately. You can learn more about Asirra here MSR Asirra: A Human Interactive Proof.

How to use it

You can easily use it in your website. You can find details of how to use it from MSR Asirra Project – Installation.

I have prepared a helper to use it in ASP.Net MVC application and I will show how to use this helper in your website. First look at the following sample pictures of user registration form.

Integrate Asirra challenge

Import the namespace at the top of your view aspx page.

<%@ Import Namespace="Valentica.Helpers" %>

Then call the GenerateAsirra() method of the helper where you like to put the Asirra challenge. For example:

<%= AsirraHelper.GenerateAsirra() %>
 
// Or
// You can control where the big version of the photos appear by
// changing EnlargedPosition to Top, Bottom, Left, or Right
<%= AsirraHelper.GenerateAsirra(EnlargedPosition.Right) %>
 
// Or
// You can control the aspect ratio of the box by changing rowsPerCell value 
<%= AsirraHelper.GenerateAsirra(4) %>
 
// Or
<%= AsirraHelper.GenerateAsirra(EnlargedPosition.Right, 4) %>

Perform client side validation

To validate Asirra in client side use the following steps.

Step 1:

Call MySubmitForm() method in onsubmit properties of your form. For example:

<form id="mainForm" action="/Account/Register" method="post"
         onsubmit="return MySubmitForm();">

Step 2:

Then simply use the following javascript code.

<script type="text/javascript">
    var passThroughFormSubmit = false;
 
    function MySubmitForm()
    {
         if (passThroughFormSubmit) {
              return true;
         }
         // Do site-specific form validation here, then...
         Asirra_CheckIfHuman(HumanCheckComplete);
         return false;
    }
 
    function HumanCheckComplete(isHuman)
    {
         if (!isHuman)
         {
              alert("Please correctly identify the cats.");
         }
         else
         {
              passThroughFormSubmit = true;
              formElt = document.getElementById("mainForm");
              formElt.submit();
         }
    }
</script>

Server side validation

In your action field together with other validation use IsValidate() method of AsirraHelper.

// Validate Assira Here
if (AsirraHelper.IsValidAsirra(Request.Form["Asirra_Ticket"]))
{
    ModelState.AddModelError("Asirra_Ticket", "Asirra Invalid");
}

Try it now!

References

1. MSR Asirra: A Human Interactive Proof
2. MSR Asirra Project – Installation

Source Codes

You can download full source code from here. Asirra Sample

convert this post to pdf.