public RecaptchaHtmlHelper(string publicKey, string hl, CaptchaTheme theme, CaptchaType type, string callback, string expiredCallback)
 {
     PublicKey       = RecaptchaKeyHelper.ParseKey(publicKey);
     Hl              = hl;
     Theme           = theme;
     Type            = type;
     Callback        = callback;
     ExpiredCallback = expiredCallback;
     if (string.IsNullOrEmpty(publicKey))
     {
         throw new InvalidOperationException("Public key cannot be null or empty.");
     }
 }
Exemplo n.º 2
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            RecaptchaResponse recaptchaResponse = new RecaptchaValidator()
            {
                PrivateKey = string.IsNullOrWhiteSpace(PrivateKey) ? RecaptchaKeyHelper.ParseKey("[reCaptchaPrivateKey]") : PrivateKey,
                RemoteIP   = filterContext.HttpContext.Request.UserHostAddress,
                Response   = filterContext.HttpContext.Request.Form["g-recaptcha-response"]
            }.Validate();

            if (!recaptchaResponse.IsValid)
            {
                ((Controller)filterContext.Controller).ModelState.AddModelError("ReCaptcha", GetErrorMessage(recaptchaResponse.ErrorCode));
            }

            filterContext.ActionParameters["captchaValid"] = recaptchaResponse.IsValid;
            base.OnActionExecuting(filterContext);
        }