public async Task <IActionResult> Create(CreateViewModel createVM)
        {
            if (!ModelState.IsValid)
            {
                return(View(createVM));
            }
            else
            {
                var captchaResponce = await _reCaptcha.Validate(Request.Form);

                if (!captchaResponce.Success)
                {
                    ModelState.AddModelError("reCaptchaError",
                                             "reCAPTCHA error occured. Please try again.");

                    return(View(createVM));
                }
            }


            var advert = _createAdvert.CreateAdvert(createVM.Content, createVM.Image, new Guid("58222fde-d3f2-4eb3-997f-08d6f101052e"));

            _operationDb.AddAdvert(advert);

            return(RedirectToAction("Index"));
        }
Example #2
0
        public override async Task <bool> ValidateModelAsync(UserRegistration registration, IUpdateModel updater)
        {
            // Get settings
            var settings = await _recaptchaSettingsStore.GetAsync();

            // If we don't have any settings we can't perform the checks
            // Ensure we return true to still allow the user to login

            if (settings == null)
            {
                return(true);
            }

            if (string.IsNullOrEmpty(settings.SiteKey))
            {
                return(true);
            }

            if (string.IsNullOrEmpty(settings.Secret))
            {
                return(true);
            }

            // Get posted response
            var recaptchaResponse = _request.Form["g-recaptcha-response"];

            // No valid posted - user has probably not ticked check box
            if (String.IsNullOrEmpty(recaptchaResponse))
            {
                updater.ModelState.AddModelError(string.Empty, "You must indicate your not a robot!");
                return(false);
            }

            // Validate posted recaptcha response
            var response = await _recaptchaService.Validate(recaptchaResponse);

            // No response received
            if (response == null)
            {
                updater.ModelState.AddModelError(string.Empty,
                                                 "A problem occurred communicating with the Google reCAPTCHA service. Please try again. If the problem persists please contact us.");
                return(false);
            }

            // Configuration issues?
            if (response.ErrorCodes.Count > 0)
            {
                updater.ModelState.AddModelError(string.Empty,
                                                 "A configuration error with Google reCAPTCHA has occurred. Response from Google: " +
                                                 String.Join(Environment.NewLine, response.ErrorCodes.Select(c => c).ToArray()));
                return(false);
            }

            // Response failed
            if (!response.Succeeded)
            {
                updater.ModelState.AddModelError(string.Empty,
                                                 "The Google reCAPTCHA service could not validate you are human. If this is incorrect please contact us.");
                return(false);
            }


            return(true);
        }