Support for new version reCAPTCHA / V2

1 year ago
I was on 3.7. I figured if I was doing the work I'd go to 4.

I ran the 3.7-3.8 script. I ran the 3.8-3.9 script. I ran the 3.9-4.0 script.

I pulled the site contents down via git (I'm running in Azure). I deleted everything. Copied in all the 4.00 stuff. Copied back my two .txt files as per the instructions. Committed and pushed back to the server.

That all seemed to work - the site came up, I can see my previous orders, customers, products.

I just checked now and it finally does appear to have updated the captcha setting. Perhaps there's some data caching that takes time to clear/refresh?

Now at least my error is about keys - so I must have mis-entered the new v2 keys and I'll need to try them again.

I assumed private=private and public=secret, but maybe it is the other way around?
1 year ago
Glad to hear it's working finally if it was just a caching issue. And...

Nop's "public" key = reCaptcha "site" key
Nop "private" key = reCaptcha "secret" key
1 year ago
gyn 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;

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;
}
11 months ago
Hi all
and happy new year
Does 4.10 support captcha v3?
If it does, how can we use it? cause there is no dropdown list to select version in demo 4.10
http://admin-demo.nopcommerce.com/Admin/Setting/GeneralCommon

thanks in advanceac
11 months ago
HI All,

we are using Nop V2.60 version, is it possible to convert reCAPTCHA V1 to V2. if possible how can I follow the steps.

Thanks,
11 months ago
venkatgoud wrote:
we are using Nop V2.60 version, is it possible to convert reCAPTCHA V1 to V2. if possible how can I follow the steps

Download one of the next version (e.g. 3.90) and see how it's implemented there. Then do the same in your 2.60
9 months ago
Hi there,

Does 4.10 support captcha v3?
4 months ago
Hi,

When I use nopcommerce 3.9, recaptcha has no error when submitting the form. I have upgraded to nopcommerce 4.2 and feel recaptcha errors regularly. I submit the form 10 times, the error is 8 times or 9 times and only 1 time.
I use the login form, contact us etc. by default nopcommerce.

Has anyone ever encountered this problem with nopcommerce version 4.2. I install nopcommerce 4.2
2 months ago
For anyone looking for a quick fix (for version 3.6), I have uploaded the files you need to change to my Github. I obviously followed the instructions from this post - but it still took me way longer than it should have to figure it all out.

I also included instructions if you want to do it from scratch and republish/recompile NOP Commerce yourself.
2 months ago
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.