1. Register reCAPTCHA API Key:
Get your Site Key and Secret Key.
Go to the Google reCAPTCHA Admin Console and register your site.
2. Client-Side (Frontend) Integration:
Add the following code to the aspx page where you want the reCAPTCHA widget to appear:
htmlCopy code<form id="form1" runat="server">
https://www.google.com/recaptcha/api.js
<div class="g-recaptcha" data-sitekey="Your-Site-Key"></div>
<input type="submit" value="Submit" />
</form>
- Replace
Your-Site-Keywith your actual Site Key. - This will display the default Google reCAPTCHA v2 widget.
3. Server-Side Validation:
In your code-behind, validate the user’s response by sending a POST request to Google’s reCAPTCHA API with the user’s response token and your Secret Key.
Here’s an example in C#:
csharpCopy codeusing System.Net;
using System.IO;
using Newtonsoft.Json;
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
string captchaResponse = Request["g-recaptcha-response"];
var result = ValidateCaptcha(captchaResponse);
if (result.success)
{
// Proceed with form processing
}
else
{
// Handle failed validation
}
}
}
public class ReCaptchaResponse
{
public bool success { get; set; }
public string challenge_ts { get; set; }
public string hostname { get; set; }
}
private ReCaptchaResponse ValidateCaptcha(string response)
{
var secretKey = "Your-Secret-Key";
var requestUri = $"https://www.google.com/recaptcha/api/siteverify?secret={secretKey}&response={response}";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(requestUri);
req.Method = "POST";
using (WebResponse resp = req.GetResponse())
{
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
string jsonResponse = sr.ReadToEnd();
return JsonConvert.DeserializeObject<ReCaptchaResponse>(jsonResponse);
}
}
}
- Replace
Your-Secret-Keywith your actual Secret Key.
Summary:
- Add the widget to your page by using Google’s
g-recaptchaelement. - Validate the response server-side by making a POST request to Google’s reCAPTCHA API.
- Customize the widget using
data-*attributes. - Optionally, install a NuGet package for ASP.NET to help with validation.
- For example – refer below you tube video from Sudhanshu Jain







