Copied to clipboard

Flag this post as spam?

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


  • James Purchase 4 posts 74 karma points
    Oct 10, 2019 @ 06:20
    James Purchase
    0

    Hi,

    I'm trying to implement reCaptcha version 2 on my umbraco v7 build that is already live. Now I have the client-side verification working and would like to verify the key that's provided with google. It would be ideal if I could do this with javascript but would be grateful for any solution that would work.

    Thanks in advance for any help you can provide. James

  • James Purchase 4 posts 74 karma points
    Oct 10, 2019 @ 07:06
    James Purchase
    0

    Forgot to say i'm on Umbraco version 7.6.4 and i can't upgrade right now due to custom document types etc.

  • George Phillipson 61 posts 176 karma points
    Oct 10, 2019 @ 08:25
    George Phillipson
    0

    Hi James

    Have you tried Install-Package RecaptchaNet -Version 2.1.0

    I then create a MasterController and add the following:

    public class MasterSurfaceController : SurfaceController
        {
            public bool IsReCapthaValidGetRecaptchaVerificationHelper()
            {
                RecaptchaVerificationHelper recaptchaHelper = RecaptchaMvcExtensions.GetRecaptchaVerificationHelper();
    
                bool isvalid = !string.IsNullOrEmpty(recaptchaHelper.Response);
    
                return isvalid;
            }
    
            public async Task<bool> IsReCapthaValidVerifyRecaptchaResponseTaskAsync()
            {
                RecaptchaVerificationHelper recaptchaHelper = RecaptchaMvcExtensions.GetRecaptchaVerificationHelper();
                RecaptchaVerificationResult recaptchaResult = await recaptchaHelper.VerifyRecaptchaResponseTaskAsync();
    
                bool isvalid = recaptchaResult == RecaptchaVerificationResult.Success;
    
                return isvalid;
            }
        }
    

    You may have to change the namespaces as I modified the code that I use to fit into what I needed.

    In the controller add:

    bool isRecaptchaValid = IsReCapthaValidGetRecaptchaVerificationHelper();
                    bool isRecaptchaResponseValid = await IsReCapthaValidVerifyRecaptchaResponseTaskAsync();
    
                    if (!isRecaptchaValid)
                    {
    
                        ModelState.AddModelError("", "Recaptcha cannot be empty");
                    }
    
                    if (!isRecaptchaResponseValid)
                    {
    
                        ModelState.AddModelError("", "Recaptcha answer incorrect");
                    }
    

    Then in the view, just add:

    @Html.Recaptcha(dataSize: RecaptchaDataSize.Normal, theme: RecaptchaTheme.Blackglass)
    

    And if you are using jQuery/Ajax, don't forget to add: window.grecaptcha.reset();

    Regards George

  • James Purchase 4 posts 74 karma points
    Oct 10, 2019 @ 09:18
    James Purchase
    0

    Thank you so much for your response, how can I install RecaptchaNet if I don't have access to VS on the server? can you install this manually?

  • George Phillipson 61 posts 176 karma points
    Oct 10, 2019 @ 09:29
    George Phillipson
    0

    Hi James

    Just install it on your development machine, and then push up as you would any other changes.

    I forgot to mention when you install it, it adds the following to your web.config.

    <add key="recaptchaPublicKey" value="" />
    <add key="recaptchaPrivateKey" value="" />
    <add key="recaptchaApiVersion" value="2" />
    

    Just add your keys in the value section.

    Regards George

  • James Purchase 4 posts 74 karma points
    Oct 10, 2019 @ 10:27
    James Purchase
    0

    Thanks, I will do this asap, I will post here if I have any issues and thanks again you are a lifesaver!

Please Sign in or register to post replies

Write your reply to:

Draft