Home  »  Articles  »  ASP.Net  »

Using Google reCAPTCHA in ASP.Net

 Posted By : Manoranjan SahooPosted Date : 18/12/2014 06:10:49 PM Category : ASP.Net Points : 10

Background :
This article explains the using of Google reCAPTCHA API in ASP.Net website to prevent input from an automated system. Let us learn about it step-by-step.
 
What is reCAPTCHA?
This is a free service from Google that helps to protect websites from spam and abuse that restricts the automated input sent by a system and allows only input from a real human.
 
To use it in asp.net website, follow below steps :
 
Step 1 :
Create a simple web application to show the use of reCAPTCHA.
  1. Open Microsoft Visual Studio 2010.
  2. Goto File --> New WebSite --> C# --> Empty WebSite (to avoid adding a master page).
  3. Provide a name for the new website such as "reCaptcha" or another as you wish and specify the location.
  4. Then right-click on the website name in Solution Explorer --> Add New Item. Then select  Add Web Form.
  5. Drag and drop one button and label and two Textboxes onto the <form> section of the Default.aspx page.
Now the default.aspx page source code will look as below.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="reCaptcha.Default" %>  
  
<!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">  
UserId: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br>
Password:<asp:TextBox TextMode="Password" ID="TextBox2" runat="server"></asp:TextBox></td>
    <asp:Button ID="btnLogin" runat="server" Text="Submit" OnClick="btnLogin_Click" /><br />  
    <asp:Label ID="lblResult" runat="server" Text="Label"></asp:Label>  
    </form>  
</body>  
</html>

Step 2:

Now download the reCaptcha .Net library from below link :
 
Step 3:
 Add a reference for the downloaded library Recaptcha.dll to the web application as in the following:
  1. Right-click on the web application, then click on Add reference.
  2. After Add reference choose Browse then find the location of the downloaded library.
  3. Select Recaptcha.dll and click on OK.
  4. After adding the Recaptcha.dll reference into the web application, the bin folder will look such as below.
 recaptcha
 
Step 4 :
Add the Recaptcha control page header to the ASP.NET Page as in the following:
 <%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>
Step 5 :
Place the Google Recaptcha control on the Default.aspx page under the form tag as:
<recaptcha:RecaptchaControl  
    ID="recaptcha"  
    runat="server"  
    PublicKey="your_public_key"  
    PrivateKey="your_private_key"  
    />

 

Properties of RecaptchaControl:
The RecaptchaControl provides many properties. But the following are some of the common properties:

PublicKey: This is a mandatory property that valiadates the user request at the client-side. This is unique key is provided by Google for the web site with respect to the web site domain.

PrivateKey: This is a mandatory property that valiadates the user request at the server-side with the Google server. This is a unique secret key provided by Google for the web site with respect to the web site domain basically for communicating between our server and the Google server.

ErrorMessage: This is an optional property that sets an error message when the user enters an invalid CAPTCHA.

AllowMultipleInstances: This is an optional property that decides the number of CAPTCHA code generations at a time. By default, it is false. If it is set to true then two CAPTCHA codes are generated for a single request.

Theme: This is used to set the background color for the CAPTCHA control. Ex : dark, light
Now you can get the private key and public key from google and replace with the above code.

Step 6:

 
Now open the default.aspx.cs file and add the following code to the OnClick event of the btnLogin button to ensure that the CAPTCHA code is validated successfully:
protected void btnLogin_Click(object sender, EventArgs e)  
   {  
       if (recaptcha.IsValid)  
       {  
           lblResult.Text = "Login Sucesfully and Captcha Validated Successfully";  
           lblResult.ForeColor = Color.Green;  
        }  
       else  
       {  
  
           lblResult.Text = "The captcha verification is unsuccessful";  
           lblResult.ForeColor = Color.Red;  
       }  
   }

Now run the application.

 
Responses
No responses found for this thread. Be the first to respond this thread.
Post Reply
Login to post Response.