Copied to clipboard

Flag this post as spam?

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


  • Arther 3 posts 73 karma points notactivated
    Mar 18, 2020 @ 12:32
    Arther
    0

    reCAPTCHA V2 with MVC in Umbraco v8 - Form submit without recaptcha validation

    Hi Gurus,

    I am so frustrated with this reCAPTCHA V2 using MVC in Umbraco v8.x.x. It does not check or validate recaptcha at all. It simply submit the form with no message. Please help.

    I used recent latest video of creating Contact form and client dependency Umbraco v8 on youtube, When i submit the form it doesn't validate the Recaptha field.

    using Project.Core.Services;
    using Project.Core.ViewModels;
    using Recaptcha.Web;
    using Recaptcha.Web.Mvc;
    using System.Web;
    using System.Web.Mvc;
    using Umbraco.Web;
    using Umbraco.Web.Mvc;
    
    namespace Project.Core.Controllers
    {
        public class ContactSurfaceController : SurfaceController
        {
            private readonly ISmtpService _smtpService;
    
            public ContactSurfaceController(ISmtpService smtpService)
            {
                _smtpService = smtpService;
            }
    
            [HttpGet]
            public ActionResult RenderForm()
            {
                ContactViewModel model = new ContactViewModel() { ContactFormId = CurrentPage.Id };
                return PartialView("~/Views/Partials/Contact/contactForm.cshtml", model);
            }
    
            [HttpPost]
            public ActionResult RenderForm(ContactViewModel model)
            {
                return PartialView("~/Views/Partials/Contact/contactForm.cshtml", model);
            }
    
            [HttpPost]
            public ActionResult SubmitForm(ContactViewModel model)
            {
    
                if (!ModelState.IsValid)
                {
                    return CurrentUmbracoPage();
    
                }
    
                RecaptchaVerificationHelper recaptchaHelper = this.GetRecaptchaVerificationHelper();
                if (string.IsNullOrEmpty(recaptchaHelper.Response))
                {
                    ModelState.AddModelError("reCAPTCHA", "Please complete the reCAPTCHA");
                    return CurrentUmbracoPage();
                }
                else
                {
                    RecaptchaVerificationResult recaptchaResult = recaptchaHelper.VerifyRecaptchaResponse();
                    if (recaptchaResult != RecaptchaVerificationResult.Success)
                    {
                        ModelState.AddModelError("reCAPTCHA", "The reCAPTCHA is incorrect");
                        return CurrentUmbracoPage();
                    }
                }
    
                bool success = false;
    
                if (ModelState.IsValid)
                {
                    success = _smtpService.SendEmail(model);
    
                }
    
                var contactPage = UmbracoContext.Content.GetById(false, model.ContactFormId);
                var successMessage = contactPage.Value<IHtmlString>("successMessage");
                var errorMessage = contactPage.Value<IHtmlString>("errorMessage");
    
                return PartialView("~/Views/Partials/Contact/result.cshtml", success ? successMessage : errorMessage);
    
            }
        }
    }
    

    and my View Model:

            public class ContactViewModel
            {
                [Required(ErrorMessage = "Please enter your name")]
                public string Name { get; set; }
    
                [Required(ErrorMessage = "please enter your email address")]
                [EmailAddress(ErrorMessage = "you must enter a valid email address")]
                public string Email { get; set; }
    
                [Required(ErrorMessage = "please enter your message")]
                [MaxLength(length: 500, ErrorMessage = "Your message must be no longer than 500 characters")]
                public string Message { get; set; }
    
                @*[Required(ErrorMessage = "please confirm you are human")]
                public string ErrorMessageCaptcha { get; set; }*@
    
                public int ContactFormId { get; set; }
            }
    

    and my Partial View

    @using (Ajax.BeginForm("SubmitForm", "ContactSurface", new AjaxOptions() { UpdateTargetId = "form-result", HttpMethod = "POST", InsertionMode = InsertionMode.Replace, OnSuccess = "contactForm.showResult", OnFailure = "contactForm.showResult" }, new { id = "contact-form" })) { @Html.HiddenFor(m => m.ContactFormId) @*@Html.AntiForgeryToken()*@
    @Html.TextBoxFor(m => m.Name, new { @class = "form-control", placeholder = "Your firstname" }) @Html.ValidationMessageFor(m => m.Name)
    @Html.TextBoxFor(m => m.Email, new { @class = "form-control", placeholder = "Your email address" }) @Html.ValidationMessageFor(m => m.Email)
    @Html.TextAreaFor(m => m.Message, new { @class = "form-control", placeholder = "Write us something", cols = "30", rows = "6" }) @Html.ValidationMessageFor(m => m.Message)
    @Html.Recaptcha(theme: Recaptcha.Web.RecaptchaTheme.Clean) @*@Html.ValidationMessageFor(m => m.ErrorMessageCaptcha)*@
    @*
    *@
    }
Please Sign in or register to post replies

Write your reply to:

Draft