Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home CSharp Implement Google Captcha (reCAPTCHA) With ASP.NET

Implement Google Captcha (reCAPTCHA) With ASP.NET

Before using Google Captcha (reCAPTCHA), you need to Create a reCAPTCHA key by providing a domain name via http://www.google.com/recaptcha/whyrecaptcha. Let’s store the Public Key and Private Key in the appSettings section of the web.config then we will read these appSetting in code behind. Your web.config is similar to below:

<?xml version="1.0"?>
 
<configuration>
 
    <system.web>
        <compilation debug="false" targetFramework="4.0" />
    </system.web>
 
    <appSettings>
        <add key="reCAPTCHAPublicKey" value="your_public_key"/>
        <add key="reCAPTCHAPrivateKey" value="your_private_key"/>
    </appSettings>
 
</configuration>

<?xml version="1.0"?> <configuration> <system.web> <compilation debug="false" targetFramework="4.0" /> </system.web> <appSettings> <add key="reCAPTCHAPublicKey" value="your_public_key"/> <add key="reCAPTCHAPrivateKey" value="your_private_key"/> </appSettings> </configuration>

You also need to download the Google Captcha (reCAPTCHA) ASP.NET library via http://code.google.com/p/recaptcha/downloads/list?q=label:aspnetlib-Latest.

After got the reCAPTCHA keys and downloaded the reCAPTCHA ASP.NET library, let’s add a reference to the Recaptcha.dll on your web application.

Below is full source code that places a Google Captcha on a asp.net page:

C# – ASP.NET Implement Google Captcha

+ aspx source code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="google-captcha.aspx.cs" Inherits="google_captcha" %>
 
<%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>
 
<!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">
    <div>
        <asp:Label Visible="false" ID="lblResult" runat="server" />
        <recaptcha:RecaptchaControl ID="recaptcha" runat="server" />
 
        <asp:Button ID="btnSubmit" runat="server" Text="Test Google Captcha" OnClick="btnSubmit_Click" />
    </div>
    </form>
</body>
</html>

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="google-captcha.aspx.cs" Inherits="google_captcha" %> <%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %> <!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"> <div> <asp:Label Visible="false" ID="lblResult" runat="server" /> <recaptcha:RecaptchaControl ID="recaptcha" runat="server" /> <asp:Button ID="btnSubmit" runat="server" Text="Test Google Captcha" OnClick="btnSubmit_Click" /> </div> </form> </body> </html>

+ aspx.cs source code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class google_captcha : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            recaptcha.PublicKey = System.Configuration.ConfigurationManager.AppSettings["reCAPTCHAPublicKey"].ToString();
            recaptcha.PrivateKey = System.Configuration.ConfigurationManager.AppSettings["reCAPTCHAPrivateKey"].ToString();
        }
    }
 
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (recaptcha.IsValid)
            lblResult.Text = "Correct!!!";
        else
            lblResult.Text = "Incorrect!!!";
 
        lblResult.Visible = true;
    }
}

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class google_captcha : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { recaptcha.PublicKey = System.Configuration.ConfigurationManager.AppSettings["reCAPTCHAPublicKey"].ToString(); recaptcha.PrivateKey = System.Configuration.ConfigurationManager.AppSettings["reCAPTCHAPrivateKey"].ToString(); } } protected void btnSubmit_Click(object sender, EventArgs e) { if (recaptcha.IsValid) lblResult.Text = "Correct!!!"; else lblResult.Text = "Incorrect!!!"; lblResult.Visible = true; } }

Google Captcha - reCAPTCHA

Google Captcha - reCAPTCHA

If we need a simple captcha like Math Captcha, take a look on how to implement Math Captch with ASP.NET (C#)

Feb 19, 2012Hoan Huynh

Source Code Demo page

Install ASP On Microsoft Windows Server 2008 IIS 7.0 and IIS 7.5PHP Check If User Like Facebook Page
You Might Also Like:
  • Implement Google Captcha (reCAPTCHA) With PHP
  • When we need to implement CAPTCHA
  • Create Simple Math (Mathematical) Captcha With ASP.NET C#
  • Asp.net Gridview Simple Example
  • System.Security.SecurityException: That assembly does not allow partially trusted callers
  • Create Auto Refresh Page Or Redirect After A Given Seconds
  • C# ASP.NET Remove leading and trailing quotes(‘) or double quotes (“)
  • JQuery How to use DatePicker
  • C# How to Get key/value pairs from app.config and web.config
  • PHP Google Analytics API Examples
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

9 years ago CSharpAppSettings, CAPTCHA, ConfigurationManager, Google, Math Captcha, reCAPTCHA5,443
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
22,485 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
20,284 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
16,043 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,519 views
C# Read Json From URL And Parse/Deserialize Json
10,066 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Online Payday Loans – Learn How To Make the Most of A Alternative Lending Option
  • Strategies For Buying Photo Editor Software
  • Where to Find the Greatest Free Photo Editor on the Web
  • Custom Research Paper – What’s it So Useful?

  • Getting Bad Credit Paydayloans From a Reputable Source
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (247)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development