The following are the highlights of the library:
- Renders reCAPTCHA widget and verifies reCAPTCHA response with minimal amount of code
- Provides reCAPTCHA web control (ASP.NET Web Forms for .NET Framework 4.5 and above)
- Provides HTML helper to quickly render reCAPTCHA widget (ASP.NET MVC 5)
- Supports reCAPTCHA version 2
- Supports reCAPTCHA callback and reCAPTCHA expired callback
Before you can use reCAPTCHA in your web application, you must first create a reCAPTCHA API key (a pair of site and secret keys). Creating reCAPTCHA API key is very straight-forward. The following are the steps:
- Go to the Google's reCAPTCHA site.
- Click on the Admin Console menu option. You will be required to login with your Google account.
- In the Admin Console page, click on the Create button.
- Enter a label for your web application.
- Select reCAPTCHA v2 option and then "I'm not a robot" Checkbox sub-option from the reCAPTCHA Type list.
- Enter the domain of your web application, e.g. example.com. If you are creating this key for your localhost, just enter localhost. You can enter more than one domain which is useful if you want the key to work across different hosts.
- Accept the reCAPTCHA terms of service.
- Click on the Submit button.
- Copy your Site Key and Secret Key which you would need to specify in your application's web.config file.
The best and the recommended way to install the latest version of reCAPTCHA for .NET is through Nuget. From the Nuget's Package Manager Console in your Visual Studio .NET IDE, simply execute the following command:
PM> Install-Package RecaptchaNet
You can also download a released build of reCAPTCHA for .NET by going to the Releases section of this project.
ASP.NET Web Forms / ASP.NET MVC 5
In the appSettings section of your web.config file, add the following keys:
<appSettings>
<add key="RecaptchaSiteKey" value="Your site key" />
<add key="RecaptchaSecretKey" value="Your secret key" />
</appSettings>
ASP.NET
In appsettings.json, add the following JSON properties:
"RecaptchaSiteKey": "Your site key",
"RecaptchaSecretKey": "Your secret key"
In the ConfigureServices method of the Startup class, add the following line of code:
using Recaptcha.Web.Configuration;
...
RecaptchaConfigurationManager.SetConfiguration(Configuration);
You can either use the Recaptcha.Web.UI.Controls.RecaptchaWidget web control (ASP.NET Web Forms) or call the RecaptchaWidget method of HTML helper (ASP.NET MVC 5) to render reCAPTCHA widget:
ASP.NET Web Forms
<%@ Register Assembly="Recaptcha.Web" Namespace="Recaptcha.Web.UI.Controls" TagPrefix="cc1" %>
...
<cc1:RecaptchaWidget ID="Recaptcha1" runat="server" />
ASP.NET MVC 5
@using Recaptcha.Web.Mvc;
...
@Html.RecaptchaWidget()
The above code by default renders both the API script as well as the widget. There are times when you want to render the API script and the widget separately such as the need to render multiple widgets on a page. The following is an example of how to achieve this:
ASP.NET Web Forms
<%@ Register Assembly="Recaptcha.Web" Namespace="Recaptcha.Web.UI.Controls" TagPrefix="cc1" %>
...
<cc1:RecaptchaApiScript ID="RecaptchaApiScript1" runat="server" />
<cc1:RecaptchaWidget ID="RecaptchaWidget1" RenderApiScript="false" runat="server" />
<cc1:RecaptchaWidget ID="RecaptchaWidget2" RenderApiScript="false" runat="server" />
ASP.NET MVC 5
@using Recaptcha.Web.Mvc;
...
@Html.RecaptchaApiScript()
@Html.RecaptchaWidget(renderApiScript:false)
@Html.RecaptchaWidget(renderApiScript:false)
When your end-user submits the form that contains the reCAPTCHA widget, you can easily verify reCAPTCHA response with few lines of code:
ASP.NET Web Form
if (String.IsNullOrEmpty(Recaptcha1.Response))
{
lblMessage.Text = "Captcha cannot be empty.";
}
else
{
var result = Recaptcha1.Verify();
if (result.Success)
{
Response.Redirect("Welcome.aspx");
}
else
{
lblMessage.Text = "Error(s): ";
foreach(var err in result.ErrorCodes)
{
lblMessage.Text = lblMessage.Text + err;
}
}
}
ASP.NET MVC 5
using Recaptcha.Web.Mvc;
...
RecaptchaVerificationHelper recaptchaHelper = this.GetRecaptchaVerificationHelper();
if (String.IsNullOrEmpty(recaptchaHelper.Response))
{
ModelState.AddModelError("", "Captcha answer cannot be empty.");
return View(model);
}
RecaptchaVerificationResult recaptchaResult = recaptchaHelper.VerifyRecaptchaResponse();
if (recaptchaResult != RecaptchaVerificationResult.Success)
{
ModelState.AddModelError("", "Incorrect captcha answer.");
}
The attributes are used to control the behavior and appearance of the reCAPTCHA widget. They are specified in one of the three ways:
- As API parameters (ASP.NET MVC helper methods)
- As properties of a web control (ASP.NET Web Control)
- Configuration (web.config / appsettings.json)
Assigning a value through method or property takes precedence over configuration. Of course, you don't need to set any attribute anywhere unless its required. The following is the entire list of the attributes:
Attribute | Description | Type | Values | Default Value | Configuration Key | Required |
---|---|---|---|---|---|---|
Site Key | Site key for reCAPTCHA. It is required for rendering the widget. | String |
The site key associated with the site you register in Google reCAPTCHA Admin Console. | No default value. Must be provided. | RecaptchaSiteKey |
Yes |
Secret Key | Secret key for the reCAPTCHA. It is required for verifying reCAPTCHA response. | String |
The secret key associated with the site you register in Google reCAPTCHA Admin Console. | No default value. Must be provided. | RecaptchaSecretKey |
Yes |
APIVersion | Determines the version of the reCAPTCHA API. | String |
- | 2 | RecaptchaApiVersion |
No |
Language | Forces the reCAPTCHA widget to render in a specific language. By default, the user's language is used. | String |
One of the values from the Language Codes list. | User's language | RecaptchaLanguage |
No |
Size | The size of the reCAPTCHA widget. | RecaptchaSize enum |
Default , Normal , Compact |
Default |
RecaptchaSize |
No |
TabIndex | The tabindex of the reCAPTCHA widget. | Int32 |
Any integer | 0 | - | No |
Theme | The colour theme of the reCAPTCHA widget. | RecaptchaTheme enum |
Default , Light , Dark |
Default |
RecaptchaTheme |
No |
Use SSL | Determines if SSL is to be used in Google reCAPTCHA API calls. | RecaptchaSslBehavior enum |
AlwaysUseSsl , SameAsRequestUrl , DoNotUseSsl |
AlwaysUseSsl |
RecaptchaUseSsl |
No |
Callback | The name of your callback function, executed when the user submits a successful response. The g-recaptcha-response token is passed to your callback. | String |
The name of a globally accessible function | No default value. | RecaptchaCallback |
No |
Expired Callback | The name of your callback function, executed when the reCAPTCHA response expires and the user needs to re-verify. | String |
The name of a globally accessible function | No default value | RecaptchaExpiredCallback |
No |