Orchardcore recaptcha

From CrestApps/CrestApps.AgentSkills · View interactive version

Orchardcore recaptcha is an AI agent skill from the GitHub repository CrestApps/CrestApps.AgentSkills. Skill for protecting Orchard Core endpoints and forms with Google reCAPTCHA. Covers ReCaptchaSettings through IOptionsMonitor, the OrchardCore ReCaptcha configuration section, ReCaptchaShape, the ReCaptcha shape, the captcha tag helper, form protection, controller validation, recipe configuration, a

How to use this skill

Skill file

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 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

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.