Files
LeafWeb/WebCms/Controllers/ValidateRecaptchaAttribute.cs
T

30 lines
1.0 KiB
C#

using System.Linq;
using System.Web;
using System.Web.Http.Results;
using System.Web.Mvc;
using hbehr.recaptcha;
using log4net;
using Umbraco.Web.Mvc;
namespace LeafWeb.WebCms.Controllers
{
public class ValidateRecaptchaAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
if (context != null)
{
//https://github.com/henriqb/ReCaptcha-Asp-Net
//https://stackoverflow.com/questions/58328542/trying-the-samesite-attribute-fix-for-the-google-recaptcha-v2-warning-on-chrome
var userResponse = context.RequestContext.HttpContext.Request.Params["g-recaptcha-response"];
var validCaptcha = userResponse != null && ReCaptcha.ValidateCaptcha(userResponse);
if (!validCaptcha)
{
context.Result = new System.Web.Mvc.RedirectResult("/error");
}
}
base.OnActionExecuting(context);
}
}
}