Tuesday, May 20, 2014

Use Google Captcha in your ASP.NET Project in C#

Using captcha in your site making your site much better secure against hackers and their scripts. So in this article I will show you how to use Google recaptcha in your asp.net project.

First download the ReCaptcha.dll and add as a reference in your project. You can download this from here. Before starting this you need to register yourself to get a public & private key of your Google apps.

Now add the register code in your aspx page.


 <%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>

And now add the following code in your aspx code, where you want to place your captcha.


<asp:Label Visible="false" ID="lblResult" runat="server" />
 
<recaptcha:recaptchacontrol id="recaptcha" runat="server" publickey="<Your public key>"
privatekey="<Your private key>" Theme="clean" />

In the code behind on a button click add the the code..


protected void btnSubmit_Click(object sender, EventArgs e)
{
      recaptcha.Validate();
      if(Page.IsValid && recaptcha.IsValid)
      {
           lblResult.Text = "You Got It!";
           lblResult.ForeColor = Drawing.Color.Green;
      }
      else
      {
           lblResult.Text = "Incorrect";
           lblResult.ForeColor = Drawing.Color.Red;
      }     
}

Your captcha is ready. Run the project and have fun.

Output :

Download the source code here.


0 comments:

Post a Comment

Popular Posts

Pageviews