Orchard Core ReCaptcha
The OrchardCore.ReCaptcha feature supplies Google reCAPTCHA rendering and
verification. Add OrchardCore.ReCaptcha.Users when built-in user login,
registration, and password recovery pages also need protection. It is not a
replacement for authorization, rate limiting, or server-side input validation.
Guidelines
- Enable
OrchardCore.ReCaptchafor the base service, settings, and shape. - Enable
OrchardCore.ReCaptcha.Usersto protect user forms; it depends on the base feature and Users. - Enable the Users registration or reset-password features separately when those pages must be protected.
- Store the site key and secret in secure configuration for production, not source control.
- Use the
ReCaptchashape or the<captcha />tag helper to render a challenge. - Use
ValidateReCaptchaAttributeonly for form posts that actually render and submit the challenge token. - The Forms integration requires both
OrchardCore.FormsandOrchardCore.ReCaptcha. - The workflow validation task requires both
OrchardCore.WorkflowsandOrchardCore.ReCaptcha. ReCaptchaServiceverifies the server response and retries its HTTP calls through the configured resilient client.- Read
ReCaptchaSettingsthroughIOptionsMonitor<ReCaptchaSettings>.CurrentValue; settings changes apply without a shell release. - There is no IP-based robot detection,
IDetectRobots, orPreventAbusemode despite older docs; the challenge always renders when settings are configured. - Pair ReCaptcha with
OrchardCore.RateLimitsfor user endpoints (seeorchardcore-rate-limits). - All recipe JSON uses the root
{ "steps": [...] }format.
Enable the Base and Users Features
{
"steps": [
{
"name": "Feature",
"enable": [
"OrchardCore.ReCaptcha",
"OrchardCore.ReCaptcha.Users"
],
"disable": []
}
]
}
The Users feature registers IRegistrationFormEvents, ILoginFormEvent, and
IPasswordRecoveryFormEvents. With the relevant Users subfeatures enabled it
also provides display drivers for registration, forgot-password, and
reset-password forms.
Configure ReCaptcha Settings
Navigate to Settings → Security → reCaptcha and set the site key and
secret key. ReCaptchaSettings exposes SiteKey, SecretKey,
ReCaptchaScriptUri, and ReCaptchaApiUri. A usable configuration requires
the keys and verification URI (ConfigurationExists(); the old IsValid()
is obsolete). The URIs default to Google's endpoints. With
OrchardCore.Deployment, a ReCaptcha settings deployment step exports
them.
Configure a recipe with the Settings step:
{
"steps": [
{
"name": "Settings",
"ReCaptchaSettings": {
"SiteKey": "your-site-key",
"SecretKey": "your-secret-key",
"ReCaptchaScriptUri": "https://www.google.com/recaptcha/api.js",
"ReCaptchaApiUri": "https://www.google.com/recaptcha/api/siteverify"
}
}
]
}
Override tenant settings through configuration by calling
ConfigureReCaptchaSettings() (namespace OrchardCore.ReCaptcha.Extensions)
on the OrchardCoreBuilder:
using OrchardCore.ReCaptcha.Extensions;
builder.Services
.AddOrchardCms()
.ConfigureReCaptchaSettings();
{
"OrchardCore": {
"ReCaptcha": {
"SiteKey": "your-site-key",
"SecretKey": "your-secret-key"
}
}
}
Read Settings in Code
using Microsoft.Extensions.Options;
using OrchardCore.ReCaptcha.Configuration;
namespace MyModule.Services;
public sealed class CaptchaAvailability
{
private readonly IOptionsMonitor<ReCaptchaSettings> _settings;
public CaptchaAvailability(IOptionsMonitor<ReCaptchaSettings> settings)
{
_settings = settings;
}
public bool IsConfigured()
=> _settings.CurrentValue.ConfigurationExists();
}
Do not cache IOptions<ReCaptchaSettings>.Value; it does not track settings
changes.
Protect a Custom MVC Form
Render the ReCaptcha shape in the form and validate the posted token on the action.
The attribute adds a model-state error named ReCaptcha when verification
fails, so the action must return the model when ModelState is invalid.
<form asp-action="Submit" method="post">
<input asp-for="Email" />
<shape type="ReCaptcha" language="en-US" />
<button type="submit">Submit</button>
</form>
using Microsoft.AspNetCore.Mvc;
using OrchardCore.ReCaptcha.ActionFilters;
namespace MyModule.Controllers;
public sealed class ContactController : Controller
{
[HttpPost]
[ValidateReCaptcha]
public IActionResult Submit(ContactViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
return RedirectToAction(nameof(Complete));
}
public IActionResult Complete() => View();
}
For a client-side JSON post, send the generated token in the
g-recaptcha-response request header when a workflow task consumes it.
Render the Shape
Use the shape when building Liquid or Razor output instead of hand-writing the
provider markup. The shape always renders the challenge when settings are
configured:
{% shape "ReCaptcha", language: "en-US" %}
<shape type="ReCaptcha" language="en-US" />
Forms and Workflows
In a Form content type, add the ReCaptcha form part after enabling Forms and
ReCaptcha. The module registers ReCaptchaPart and its display driver, which renders the
challenge:
<captcha language="en-US" />
The <captcha /> tag helper renders the ReCaptcha shape and accepts
optional language and onload attributes.
The Validate ReCaptcha workflow task is available only when Workflows is
also enabled. It validates the challenge submitted with the request and is
appropriate for a Forms workflow, not for bypassing server-side validation.
Upgrading from 3.x
- Inject
IOptionsMonitor<ReCaptchaSettings>and readCurrentValueinstead ofIOptions<ReCaptchaSettings>. - Rename the
OrchardCore_ReCaptchaconfiguration section toOrchardCore:ReCaptcha(the old name still works but is deprecated). - Replace
OrchardCore.ReCaptcha.Permissions.ManageReCaptchaSettingswithOrchardCore.ReCaptcha.ReCaptchaPermissions.ManageReCaptchaSettings.