Monday, October 14, 2013

How to integrate Captcha in asp.net

Introduction: In this article I have explained how we can integrate Captcha in asp.net.
Captcha

Captcha is mostly used in Sign up and contact us form. I use the Recaptcha library. You can download the Recaptcha library from given link:
After download the Recapctha library extract it. You see the option dll for 4.0 or 4.5 frameworks. Take new website, right click on your website and choose Add Reference as shown in snapshot: 
Captcha

Now click on browse and select the Recaptcha.web.dll. 
Captcha

You can also add the Captcha control to Toolbox. To use Captcha control you need a public and private key. To get the key go to below given link:

Here you are asking for login. Login with your Google/Gmail account. After login you are redirect to create captcha key, enter your domain (e.g. articlemirror.blogspot.in) and select the checkbox to use this on all domains. After that click on Create key button. You have done it. You see a page as shown below:
 
Captcha
Add caption
Now add the key in web.config file:
<configuration>
       <system.web>
              <compilation debug="true" targetFramework="4.0"/>
       </system.web>
       <appSettings>
              <add key="recaptchaPublicKey" value="Public key"/>
              <add key="recaptchaPrivateKey" value="Private key"/>
       </appSettings>
</configuration>

Add a new webform to website. Register the control on your .aspx page add the following:
<%@ Register Assembly="Recaptcha.Web" Namespace="Recaptcha.Web.UI.Controls" TagPrefix="cc1" %>

If you added the control in toolbox than drag and drop it to page. If not added to toolbox than add the following to page:
<cc1:Recaptcha ID="Recaptcha" runat="server" />

Design the page.aspx as mention below:
<form id="form1" runat="server">
    <div>
    <table align="center"><tr><td>
   <cc1:Recaptcha ID="Recaptcha" runat="server" /></td><td><asp:Label ID="Label1" runat="server" Text=""></asp:Label></td></tr>
   <tr><td>
   <asp:Button ID="btncaptcha" runat="server" Text="Confirm" onclick="btncaptcha_Click" />       
        </td></tr></table>
    </div>
    </form>
On button click write the given code (.aspx.cs) :
using Recaptcha.Web;

protected void btncaptcha_Click(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(Recaptcha.Response))
    {
        Label1.Text = "Captcha cannot be empty.";
    }
    else
    {
        RecaptchaVerificationResult verify = Recaptcha.Verify();
        if (verify == RecaptchaVerificationResult.Success)
        {
            Label1.Text = "Captcha Match";
        }
        else
        {
            Label1.Text = "Captcha information not matched";
        }
    }
    }

In VB (.aspx.vb) :
Imports Recaptcha.Web

Protected Sub btncaptcha_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btncaptcha.Click
        If String.IsNullOrEmpty(Recaptcha.Response) Then
            Label1.Text = "Captcha cannot be empty."
        Else
            Dim verify As RecaptchaVerificationResult = Recaptcha.Verify()
            If verify = RecaptchaVerificationResult.Success Then
                Label1.Text = "Captcha Match"
            Else
                Label1.Text = "Captcha information not matched"
            End If
        End If
    End Sub


Now run the project and checked out the result.

Is it helpful?

If yes post your comment to admire my work. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

7 comments:

  1. WOW just what I was looking for. Came here by searching for business services

    Feel free to surf to my web page; Life advice (myfotowiki.de)

    ReplyDelete
  2. Hmm is anyone else encountering problems with the pictures on this blog loading?
    I'm trying to determine if its a problem on my end or if it's
    the blog. Any feedback would be greatly appreciated.


    My web blog: online shopping

    ReplyDelete
  3. Hi, everything is going perfectly here and ofcourse every one is sharing facts, that's genuinely excellent, keep up writing.



    My site; business advice ()

    ReplyDelete
  4. Replies
    1. you are always welcomed......keep visiting.......

      Delete