Tuesday, May 10, 2011

Capcha In ASP.NET??

What is capcha??

A CAPTCHA  is a type of challenge-response test used in computing as an attempt to ensure that the response is not generated by a computer. The process usually involves one computer (a server) asking a user to complete a simple test which the computer is able to generate and grade. Because other computers are supposedly unable to solve the CAPTCHA, any user entering a correct solution is presumed to be human.

How to implement in ASP.NET?

Step 1: In the page add the following:
<%@ Register Assembly="WebControlCaptcha" Namespace="WebControlCaptcha" TagPrefix="Capcha" %>

<div style="width: 230px;" class="greenContainer">
                            
<Capcha:CaptchaControl id="capcha" enableviewstate="false" enabletheming="false"
 captchamaxtimeout="300" runat="server" layoutstyle="Vertical" cssclass="fL" />
 
</div>


Step 2: Add the following css

.greenContainer
{border:1px solid #6a377d;background-color:#f7f2f9;overflow:auto;padding:10px;}


Step 3: In the Save button
The save operation should be done only within

if (Page.IsValid)
{}

 Step 4: Add the following in the web.config file.

<system.web>
<httpHandlers>
<add verb="GET" path="CaptchaImage.aspx" type="WebControlCaptcha.CaptchaImageHandler, WebControlCaptcha"/>
</httpHandlers>
</system.web>

This should be included after </system.web> and before <system.webServer> if exists

Also include the pages where the capcha is implemented (eg. Registration Page, Subscription)

 <location path="Subscription.aspx">
        <system.web>
            <authorization>
                <allow users="*"/>
            </authorization>
        </system.web>
    </location>

 <location path="Registration.aspx">
        <system.web>
            <authorization>
                <allow users="*"/>
            </authorization>
        </system.web>
    </location>

<location path="CaptchaImage.aspx">
        <system.web>
            <authorization>
                <allow users="*"/>
            </authorization>
        </system.web>
    </location>

Add the dll  WebControlCapcha.dll

Fixes:
If you are using IIS 7.0 then your capcha image wont display in that case do the following

Go Handler Mapping in IIS 7.0 for your application configured in IIS 7.0

Opening the Handler Mappings, the listing for all file extensions registered with various handlers was displayed, so this was the right place. However as I looked for an "Add new file mapping" option, I instead found three options
1. Add Managed Handler...
2. Add Script Map...
3. Add Module Mapping...
The Add Managed Handler is the thing that we need to work with.Back to adding managed handlers for Capcha give the parameters as below and save the parameters.
 Thatz it we have the capcha in place.

Manage Handler


 

2 comments:

  1. I lost a couple of hours trying to integrate the CAPTCHA control in a MVC application. The problem was that the routing translated CaptchaImage.aspx to a controller name. The solution is to remove it from the routes:

    routes.IgnoreRoute("CaptchaImage.aspx");

    I hope this will help someone else...

    ReplyDelete