Ejemplo n.º 1
0
        public void TestMethod2()
        {
            var recaptcha = new Recaptcha2();

            string recaptchaResponse = "03AHJ_VusuS71Y0XLyzwQTVh_inqWfUbJO_nK_SRvcsR0xfJ0T1pjq37mrwIS0crstvVA_8u36ffmZNuuUbONrS8ma6GUtM5eTaJDxs-rxO5KWCVCL_SKW9Rn4NPQWh4BH6IVvOXHoLtvaoAIp9ZUxihSkX2PNgDhJTVtNCdAvmsawevAg-FbQi-W_R-__5UeH2YqrXcjqFxDXciEyAKJXGxCzI_jknVbIWyhmTGXnd7-7_uk8u1O-aRUBi6Yb1x-qq9nRH16j1eEviXTtYZ5-qnUFueheHjk7sa7bXkIt2nxqitlH_xgDAf3rYSIMpsOfB6CAnNlBbWNiJnIU5XThERCTB4h9dI2IcuQGz4BkjoG-2A1_UjM7GCmniSh2He449pM_m1CBr1Y8pvYNvhBbA3X6ar30Ezfq7kVSGsCsPxGCzIvX6N-XuVxzjVwf9go3Nn9xEUJyZFng1AxwDrd-z6OnTsegWxTs3zHqHN3lpYVUngNRtCkkzj6hjntyVWHbWcJeV4HG2av-RrmsoLBdsuWjq8nqs61DlAKQoUawVqZW4EeiRFouL3wuS7hZu3T9SoahfUFJv7eHsiP1xqQjT4iecANhfQLF7rvEbrNoQVUMtsy0BMujZJPnW7lkZy5OC3lY7hEd0w4kCjaMj1xZW9htcf2vGXya0o1xviBc00S8fIzsxkMoyJo9ksiPQP4qAhm1f3NL2fsHfKKqvCiRKxLeD7E7zlcz_bCokjAphUFhVC4HarrYhazmXeB4-XBeWMG2wp8td2NzxFP1YIsMpl4-u6cx1dJ2cHDdbQkJepEcSub1XhYi4Z0PCJxHL_1uLDLxXCZfv-2ffcDXgZXnlCc4Gm1eHsicbLSoECfhN9_qRpjpz96gbdA9C6FfNHtg1xOkWbT5U8MpEaZbbi-xG2lEfcCH0cfiAiHiEnhBNjibjMsQE5YfLUz0NqRZR-kKgE27WhabhBWxW7VEXhzW8SWDXtb0gsKxoDVSG8kIv1aEjn8BdutSe9xaA00160E2NwV0KgPUcUwF";

            var actual = Task.Run <RecaptchaValidationResult>(async() => await recaptcha.ValidateAsync(recaptchaResponse)).Result;

            Assert.IsFalse(actual.Success);
        }
        public override async Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
        {
            if (actionContext == null && actionContext.Request == null && actionContext.Request.Headers == null)
            {
                throw new ArgumentException("actionContext is null or it does not contain HTTP request headers", nameof(actionContext));
            }

            await base.OnActionExecutingAsync(actionContext, cancellationToken);

            if (await BypassRecaptach(actionContext, cancellationToken))
            {
                return;
            }

            string recaptchaResponse = null;

            IEnumerable <string> recaptchaHeaderValues = actionContext.Request.Headers.FirstOrDefault(h => h.Key == "recaptcha").Value;

            if (recaptchaHeaderValues != null)
            {
                recaptchaResponse = recaptchaHeaderValues.FirstOrDefault();
            }

            if (string.IsNullOrWhiteSpace(recaptchaResponse))
            {
                actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, ExceptionMessages.ERR_RecaptchaNotFound);
            }
            else
            {
                Recaptcha2 recaptcha = new Recaptcha2();
                var        recaptchaValidationResult = await recaptcha.ValidateAsync(recaptchaResponse);

                if (!recaptchaValidationResult.Success)
                {
                    string errorMessage = GenerateInvalidRecaptchaErrorMessage(recaptchaValidationResult);
                    actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, errorMessage);
                }
            }

            if (!actionContext.ModelState.IsValid)
            {
                actionContext.Response =
                    actionContext.Request.CreateErrorResponse(
                        HttpStatusCode.BadRequest, actionContext.ModelState);
            }
        }