Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • AsheM 9 posts 89 karma points
    Aug 13, 2019 @ 15:39
    AsheM
    0

    Recaptcha2 not working properly with Forms

    Umbraco version 7.5.4 Forms Version: :"4.4.2"

    I have a form that you fill out the information and hit the submit button and it performs logic to email it to the proper office it needs to go to. I put the recaptcha2 question onto the form and no matter if the field is marked required or not it's still sending out the email. The rest of the fields are marked required and work properly it's just the captcha that is still allowing the post code to run. Does anyone know how to prevent this or have any ideas so I can actually have the captcha work properly? I think the form I'm trying to use it on is a Contour form if that makes a difference.

  • Shaishav Karnani from digitallymedia.com 330 posts 1562 karma points
    Aug 13, 2019 @ 16:50
    Shaishav Karnani from digitallymedia.com
    0

    Hi,

    Does recaptcha 2 is implemented properly in this case? It seems recaptcha 2 is ignored while submitting details and email is sent.

    Have you used a package for Recaptcha 2.0?

  • AsheM 9 posts 89 karma points
    Aug 13, 2019 @ 16:58
    AsheM
    0

    I was told by support that my version of forms had a bug with the recaptcha so they sent me these files:

    \appcode\ReCaptcha2.cs \AppPlugins\UmbracoForms\Backoffice\Common\FieldTypes\Recaptcha2.html \App_Plugins\UmbracoForms\Images\recaptcha2.png \Views\Partials\Forms\FieldTypes\FieldType.Recaptcha2.cshtml \Views\Partials\Forms\Themes\default\FieldTypes\FieldType.Recaptcha2.cshtml

    The cs file referenced a light and dark theme but Neither I nor the person supporting me had that theme. So I was informed to change it to clean and normal which compiled. I also had to add 'using Umbraco.Forms.Core.Attributes;' to get the cs file to compile. Am I missing files?

  • Shaishav Karnani from digitallymedia.com 330 posts 1562 karma points
    Aug 13, 2019 @ 17:08
    Shaishav Karnani from digitallymedia.com
    0

    You can apply break point at ValidateField methods in ReCaptcha2.cs and then navigate to see how it works. This method executes before saving of records and email process. Once this is successful then only rest of the process is completed. Introspecting this method will fix your issue.

    You have correct set of files but I am not sure if something is missing from ReCaptcha2.cs

    As using Umbraco.Forms.Core.Attributes; is already added in the class.

  • AsheM 9 posts 89 karma points
    Aug 26, 2019 @ 12:55
    AsheM
    0

    I don't have the project in visual studios I use the web client to modify the front of the site and Azure to work with the back end files. If I open it in visual studios and attach a debugger its going to bring the whole site down. Do you have a recommendation of how to bring the code down for a test site? I have tried a few things but its not really working.

  • Shaishav Karnani from digitallymedia.com 330 posts 1562 karma points
    Aug 26, 2019 @ 15:28
    Shaishav Karnani from digitallymedia.com
    0

    Please can share recaptcha2.cs

  • AsheM 9 posts 89 karma points
    Aug 28, 2019 @ 12:53
    AsheM
    0

    So this is what they gave me:

    using Newtonsoft.Json.Linq;
        using System;
        using System.Collections.Generic;
        using System.Net;
        using System.Web;
        using System.Linq;
        using Umbraco.Core.Logging;
        using Umbraco.Forms.Core;
        using Umbraco.Forms.Core.Attributes; 
    
        namespace WebApplication2.App_Code
    {
        public sealed class Recaptcha2 : FieldType
        {
            public Recaptcha2()
            {
                Id = new Guid("B69DEAEB-ED75-4DC9-BFB8-D036BF9D3730");
                Name = "Recaptcha2";
                FieldTypeViewName = "FieldType.Recaptcha2.cshtml";
                Description = "Google Recaptcha v2";
                Icon = "icon-eye";
                DataType = FieldDataType.String;
                SortOrder = 120;
                Category = "Simple";
            }
    
            [Setting("Theme", prevalues = "clean, normal", description = "ReCaptcha v2 theme", view = "dropdownlist")]
            public string Theme { get; set; }
    
            [Setting("Size", prevalues = "normal,compact", description = "ReCaptcha v2 size", view = "dropdownlist")]
            public string Size { get; set; }
    
            [Setting("Error message", description = "The error message to display when the user does not pass the Recaptcha check", view = "TextField")]
            public string ErrorMessage { get; set; }
    
            public override IEnumerable<string> ValidateField(Form form, Field field, IEnumerable<object> postedValues, HttpContextBase context)
            {
                var secretKey = Configuration.GetSetting("RecaptchaPrivateKey");
    
                if (string.IsNullOrWhiteSpace(secretKey))
                {
                    // just return the error message
                    var configurationError = string.Format("ERROR: ReCaptcha v2 is missing the secret key - Please update the '/app_plugins/umbracoforms/umbracoforms.config' to include 'key=\"RecaptchaPrivateKey\"'");
                    LogHelper.Warn<Recaptcha2>(configurationError);
                    return new[] { configurationError };
                }
    
                var reCaptchaResponse = HttpContext.Current.Request["g-recaptcha-response"];
                var url = string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", secretKey, reCaptchaResponse);
    
                var isSuccess = false;
                var errorCodes = new List<string>();
    
                using (var client = new WebClient())
                {
                    var response = client.DownloadString(url);
    
                    var responseParsed = JObject.Parse(response);
    
                    //Get Success Status
                    JToken sucessToken;
                    var sucessFound = responseParsed.TryGetValue("success", out sucessToken);
                    if (sucessFound)
                    {
                        isSuccess = sucessToken.Value<bool>();
                    }
    
                    //Get Error codes
                    JToken errorsToken;
                    var errorsFound = responseParsed.TryGetValue("error-codes", out errorsToken);
                    if (errorsFound)
                        errorCodes.AddRange(errorsToken.Children().Select(child => child.Value<string>()));
    
                    if (isSuccess)
                        return Enumerable.Empty<string>();
    
                    var error = field.Settings["ErrorMessage"];
                    if (string.IsNullOrWhiteSpace(error))
                        error = "Make sure to complete the \"I am not a robot\" challenge";
    
                    var errors = new List<string> { error };
                    errors.AddRange(errorCodes);
    
                    return errors;
                }
            }
    
            public override bool StoresData
            {
                get
                {
                    return false;
                }
            }       
        }
    }
    

    It seems to work. I managed to figure out how to download the files and put them into visual studios so I can sort of debug this thing. This works, but it doesn't stop my other code from running and I'm not sure how to capture the results of the captcha. The code that runs next is in a .cshtml file:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @using Umbraco.Core.Persistence;
    @using System.ComponentModel.DataAnnotations;
    @using System.Text.RegularExpressions;
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    
    @{
        Layout = "MainTemplate.cshtml";
        if (IsPost) {
    
    
            string firstname = Request.Form["63b5d8dc-7d73-4f35-b457-ba3753d1cbe3"];
            string lastname = Request.Form["455b5481-0202-4508-be02-5f1c93a1cbd5"];
    

    I attempted to use the Request.Form with the captcha ID but it didnt return a result. Any suggestions on getting this to work?

Please Sign in or register to post replies

Write your reply to:

Draft