ReCaptcha ASP.NET MVC latest version wrapper
https://www.google.com/recaptcha
1) Get Google reCAPTCHA at https://www.google.com/recaptcha
<add key="ReCaptcha:SiteKey" value="your-site-key" />
<add key="ReCaptcha:SecretKey" value="your-secret-key" />public class AccountsController : Controller
{
[HttpGet]
public ActionResult Register()
{
ViewBag.Recaptcha = ReCaptcha.GetHtml(ConfigurationManager.AppSettings["ReCaptcha:SiteKey"]);
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterMerchantViewModel request)
{
try
{
if (ModelState.IsValid && ReCaptcha.Validate(ConfigurationManager.AppSettings["ReCaptcha:SecretKey"]))
{
// Do what you need
return View("RegisterConfirmation");
}
ViewBag.RecaptchaLastErrors = ReCaptcha.GetLastErrors(this.HttpContext);
ViewBag.publicKey = ConfigurationManager.AppSettings["ReCaptcha:SiteKey"];
return View(request);
}
catch (Exception)
{
return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
}
}
}Add the following code to your Views/Merchants/Register.cshtml:
@ReCaptcha.GetHtml(@ViewBag.publicKey)
@if (ViewBag.RecaptchaLastErrors != null)
{
<div>Oops! Invalid reCAPTCHA =(</div>
}Let's talk more about the most basic way to get started:
@ReCaptcha.GetHtml("site-key")The synopsis for the @ReCaptcha.GetHtml function is:
@ReCaptcha.GetHtml(publicKey, [theme], [type], [callback], [lang])ReCaptcha Parameter reCaptcha doc
| key | value | default | description |
|---|---|---|---|
publicKey |
Your sitekey. | ||
theme |
dark/light | light | Optional. The color theme of the widget. |
type |
audio/image | image | Optional. The type of CAPTCHA to serve. |
callback |
Optional. Your callback function that's executed when the user submits a successful CAPTCHA response. The user's response, g-recaptcha-response, will be the input for your callback function. | ||
lang |
See language codes | Optional. Forces the widget to render in a specific language. Auto-detects the user's language if unspecified. |
see recaptcha doc
returns true for valid response from user, false otherwise.
privateKey 'Secret key' is Required. The shared key between your site and ReCAPTCHA.
see recaptcha doc
returns a IEnumerable<reCaptcha.ErrorCodes>.
if returns null the no errors occurred.
context is your HttpContenxt e.g. this.HttpContext