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