Support for new version reCAPTCHA / V2

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
8 years ago
venkat321 wrote:
were can i get keys for reCAPTCHA V2 ???? how do i use it in nopcommerce 3.6 ?
is it the same way like we used in reCAPTCHA V1


reCAPTCHA V2 will be available from the next version of nopCommerce (3.80). At the moment it's not available out of the box.
6 years ago
a client of mine has nopcommerce 3.4. he wants recaptcha. but google says it does not support version 1 anymore. how do i proceeed?
6 years ago
I'm running version 2.7 of nopcommerce and with the drop dead date for migrating to V2 of reCAPTCHA looming closer I need to do something or stop using it. I'd rather not stop using it. Has anyone taken this on and if so could you share what was necessary to migrate?

The post by gyn back in 2015 looked promising, but I couldn't get it to work. I didn't see mention of what version of nopcommerce the changes were made to so that could be part of my problem. Is there a fairly straight forward path to getting this done or would it just be too involved?

Thanks.
6 years ago
Bump.
6 years ago
Hello,
       I am using a nopcommerce 3.4 which have recaptcha version V1. I have done changes in CaptchaValidatorAttribute.cs and HtmlExtensions.cs and compiled the solution and copied the Nop.Web.Framework.dll and Recaptcha.Web.dll to the bin folder, Also changed the site key and privet key through admin, But i am seeing the recaptcha v1 on the UI page instead of recaptcha V2. Can someone help me out to solve this problem.

Thanks,
Akash
6 years 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


I followed the steps but i am not able to see the recaptcha version V2. I am using Nopcommerce 3.4. can you please tell me is there any changes need to do in view (where we call @Html.Raw(Html.GenerateCaptcha()) method).
Thanks,
Akash
6 years ago
Have you deleted your old keys from Google and generated new ones?
6 years 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


So has anyone created a drop-in patch for v3.6? We would be happy to purchase it, since this doesn't seem to compile correctly on our end.
6 years ago
I have implemented reCaptcha V2 for nop 3.4. It's now in the marketplace. Do you still need it for 3.6? Any other versions?
6 years ago
Thanks for asking. The code referred to in the post above that was posted by gyn near the beginning of the thread doesn't mention a version. I'm working with version 2.7. Does anyone know if it will work for that, and if not, is anything else available for that version?
Thanks.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.