Forked from carlwoodhouse/gist:e324860bdeb16d3bd6e16cf5af071954
Last active
November 17, 2017 19:35
-
-
Save LSTANCZYK/d37601b0ec568c2aa5138ec54637ea58 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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