1

Request screen shot I am trying to add reCaptcha v3 in my asp.net 5 framework project. reCaptcha is used on 2 pages, on first page it's working fine without any error, but on second page the same function return error with "invalid-input-response". On first page I am using pure mvc call and on second page I am using ajax(jquery) to submit form on which I am getting error. But in my function, I am able to receive Google reCaptcha token values from view.

Using this code to generate recaptcha

public static IHtmlString ReCaptchaHidden(this HtmlHelper helper)
        {
            var mvcHtmlString = new TagBuilder("input")
            {
                Attributes =
               {
                    new KeyValuePair<string, string>("type", "hidden"),
                    new KeyValuePair<string, string>("id", GoogleReCaptchaVariables.InputName),
                    new KeyValuePair<string, string>("name", GoogleReCaptchaVariables.InputName)
               }
            };
            string renderedReCaptchaInput = mvcHtmlString.ToString(TagRenderMode.Normal);
            return MvcHtmlString.Create($"{renderedReCaptchaInput}");
        }

        public static IHtmlString ReCaptchaJS(this HtmlHelper helper, string useCase)
        {
            string reCaptchaSiteKey = GoogleReCaptchaVariables.ReCaptchaSiteKey;
            string reCaptchaApiScript = "<script src='https://www.google.com/recaptcha/api.js?render=" + reCaptchaSiteKey + "'></script>";
            string reCaptchaTokenResponseScript = "<script>$('form').submit(function(e) { e.preventDefault(); grecaptcha.ready(function() { grecaptcha.execute('" + reCaptchaSiteKey + "', {action: '" + useCase + "'}).then(function(token) { $('#" + GoogleReCaptchaVariables.InputName + "').val(token); $('form').unbind('submit').submit(); }); }); }); </script>";
            return MvcHtmlString.Create($"{reCaptchaApiScript}{reCaptchaTokenResponseScript}");
        }

Using this code to verify recaptcha

public string ReCaptchaVerify(string responseToken)
        {
            const string apiAddress = "https://www.google.com/recaptcha/api/siteverify";
            string recaptchaSecretKey = GoogleReCaptchaVariables.ReCaptchaSecretKey;
            string responseString = null;
            var dictionary = new Dictionary<string, string>
                    {
                        { "secret", recaptchaSecretKey },
                        { "response", responseToken }
                    };
            var postContent = new FormUrlEncodedContent(dictionary);
            using (var httpClient = new HttpClient())
            {
                try
                {
                    HttpResponseMessage respons = httpClient.PostAsync(apiAddress, postContent).Result;
                    responseString = respons.Content.ReadAsStringAsync().Result;
                }
                catch
                {
                    //Todo: Error handling process goes here  
                }
            }
            return responseString;
        }

I am trying to verify google recaptcha v3 with my .net framework mvc project. Working on one place but in second place its giving error "invalid-input-response"

0