In this post I would like to discuss and share how I have integrated the Google’s prominent and renowned Captcha with Asp.Net. For more details on Google Captcha works in conjunction with websites and the advantages of using Captcha please visit the link below:
To begin with you will need to download reCAPTCHA ASP.Net Library at:
On the link above are examples on the use of the API and illuminates Asp.Net developers on how they can easily integrate Google Captcha to their websites as and when required. Please ensure you have signed (with Google account) and obtain the Keys that will be required for the code examples below.
When you add the reference to your Website/ Web Application Project the .dll namely recaptcha.dll and recaptcha.dll.refresh will be added to your BIN folder. You can also manually copy the .dll and paste them to your BIN folder.
Now, let’s begin with writing some code. Following the line of API examples in the link above, I add the following reference to the page where you would like your Captcha to be working on.
<%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>
Now, that the user control is registered, it can be used in the following way.
<recaptcha:RecaptchaControl ID="captcha" runat="server" PublicKey="YOUR_PUBLIC_KEY" PrivateKey="YOUR_PRIVATE_KEY"/>
As soon as you embed the control on the page, it is ready in action. In my case I have placed the captcha control inside the Asp Form Validation Group to makes it mandatory to be filled by end users.
Finally, there’s no guaranteeing your end users have entered the right text presented to them on the captcha. So this is where Asp CustomValidator comes handy.
<asp:CustomValidator ID="CaptchaValidator" runat="server" ErrorMessage="Please enter information correctly" Font-Bold="true" ForeColor="Red" SetFocusOnError="true" Display="Dynamic" ValidationGroup="CaptchaValidationGroup"></asp:CustomValidator>
And finally the code behind for the CustomValidator above.
Protected Sub CaptchaValidator_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CaptchaValidator.ServerValidate captcha.Validate() args.IsValid = captcha.IsValid End Sub
There you have it, a fully working Google Captcha with your Asp.Net website. On a final note, here’s something interesting,
Captcha abbreviates to “Completely Automated Public Turing test to tell Computers and Humans Apart”