Support for new version reCAPTCHA / V2

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
4 years ago
any update on this for version 4.20???
4 years ago
Getting "he reCAPTCHA response is invalid or malformed. Please try again." in version 4.2

reCaptcha is v2 and i have verified that i registered with correct parent domain and correct public / private keys are provided. Site is hosted in a sub-domain.
3 years ago
Dionis wrote:
Hi,

I have implemented this with reCAPTCHA4net from http://recaptcha4net.codeplex.com/
Demo at shop.getyournet.ch

Step 1
In Nop.Web.Framework, add a reference to the assembly Recaptcha.Web
Also add a reference to System.Net.Http

Step 2
In Nop.Web.Framework > UI > Captcha, change the file HtmlExtensions.cs

using System.IO;
using System.Web.Mvc;
using System.Web.UI;
using Nop.Core.Infrastructure;
"https://mindepcasinos.com/" rel="nofollow,ugc">mindepcasinos.com
namespace Nop.Web.Framework.UI.Captcha
{
    public static class HtmlExtensions
    {
        public static string GenerateCaptcha(this HtmlHelper helper)
        {
            var captchaSettings = EngineContext.Current.Resolve<CaptchaSettings>();

            var theme = !string.IsNullOrEmpty(captchaSettings.ReCaptchaTheme) ? captchaSettings.ReCaptchaTheme : "white";
            var captchaControl = new Recaptcha.Web.UI.Controls.Recaptcha
            {
                ID = "recaptcha",
                ColorTheme = Recaptcha.Web.ColorTheme.Light,
                PublicKey = captchaSettings.ReCaptchaPublicKey,
                PrivateKey = captchaSettings.ReCaptchaPrivateKey
            };

            var htmlWriter = new HtmlTextWriter(new StringWriter());

            captchaControl.RenderControl(htmlWriter);

            return htmlWriter.InnerWriter.ToString();
        }
    }
}


Step 3
In Nop.Web.Framework > UI > Captcha, change the file CaptchaValidatorAttribute.cs

using System.Web.Mvc;
using Nop.Core.Infrastructure;
using System;
using System.Net.Http;
using Newtonsoft.Json.Linq;
using Nop.Services.Tasks;
using System.Threading.Tasks;

namespace Nop.Web.Framework.UI.Captcha
{
    public class CaptchaValidatorAttribute : ActionFilterAttribute
    {
        private const string RESPONSE_FIELD_KEY = "g-reCAPTCHA-response";

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var valid = false;
            var captchaResponseValue = filterContext.HttpContext.Request.Form[RESPONSE_FIELD_KEY];
            if (!string.IsNullOrEmpty(captchaResponseValue))
            {
                var captchaSettings = EngineContext.Current.Resolve<CaptchaSettings>();
                if (captchaSettings.Enabled)
                    valid = System.Threading.Tasks.Task.Factory.StartNew(async () => await ValidateResponse(captchaResponseValue, captchaSettings.ReCaptchaPrivateKey).ConfigureAwait(false)).Unwrap().Result;
            }

            //this will push the result value into a parameter in our Action  
            filterContext.ActionParameters["captchaValid"] = valid;

            base.OnActionExecuting(filterContext);
        }

        private async Task<bool> ValidateResponse(string captchaResponseValue, string key)
        {
            var valid = false;
            var uribuilder = new UriBuilder("https://www.google.com/recaptcha/api/siteverify")
            {
                Query = string.Format("secret={0}&response={1}", key, captchaResponseValue)
            };
            using (var httpClient = new HttpClient())
            {
                var async = await httpClient.GetAsync(uribuilder.Uri);
                async.EnsureSuccessStatusCode();
                var readstring = await async.Content.ReadAsStringAsync();
                var resultobject = JObject.Parse(readstring);
                valid = resultobject.Value<bool>("success");
            }
            return valid;
        }
    }
}


Step 4
Compile the project and copy Nop.Web.Framework.dll and Recaptcha.Web.dll to the bin folder of your production site

Thanks a lot! I've managed to successfully include it in NopCommerce 3.60. However, the captcha generated DIV is not centered.

EDIT: You can center it by modifing the styles.css:

.captcha-box {
  text-align: center;
  line-height: 0; /*firefox line-height bug fix*/
        width: 304px;
        margin: 0 auto;
}
.captcha-box > div {
  display: inline-block;
        max-width: 100%;
        margin: auto;
}


HI!
Are there any updates on 4.20?
2 years ago
anthonycma wrote:
I recently moved my site to a shared hosting platform with 4.20 (no source). However, when reCAPTCHA enabled, on the contact page it always shows "The reCAPTCHA response is invalid or malformed. Please try again." Any solutions of this? without this, many scams coming in. please.


Same issue for me, how could i solve it? i don't want to stop using reCAPTCHA
2 years ago
Maybe this will help
https://www.nopcommerce.com/en/boards/topic/66093/nopcommerce-420-is-released/page/7#242668
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.