Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save carlwoodhouse/e324860bdeb16d3bd6e16cf5af071954 to your computer and use it in GitHub Desktop.
Save carlwoodhouse/e324860bdeb16d3bd6e16cf5af071954 to your computer and use it in GitHub Desktop.
using System.ComponentModel.DataAnnotations;
using System.Web;
using Orchard;
using Patient.Framework.AntiSpam.Services;
namespace Patient.Framework.AntiSpam.Attributes {
public class RecaptchaAttribute : ValidationAttribute {
private string _secret;
public RecaptchaAttribute(string Secret) {
ErrorMessage = "reCAPTCHA validation failed. Please ensure you are not a robot.";
_secret = Secret;
}
public override bool IsValid(object value) {
var requestContext = HttpContext.Current.Request.RequestContext;
var workContext = requestContext.GetWorkContext();
var recaptchaService = workContext.Resolve<IRecaptchaService>();
var response = requestContext.HttpContext.Request.Form["g-recaptcha-response"];
var recaptchaVerificationStatus = recaptchaService.IsValid(_secret, response, requestContext.HttpContext.Request.UserHostAddress);
if (!recaptchaVerificationStatus.IsValid && !string.IsNullOrEmpty(recaptchaVerificationStatus.ErrorMessage)) {
ErrorMessage = recaptchaVerificationStatus.ErrorMessage;
}
return recaptchaVerificationStatus.IsValid;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment