protected void InitializeServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddRecaptcha(config =>
            {
                config.SecretKey = "SecretKey";
                config.SiteKey   = "SiteKey";
                config.BackchannelHttpHandler = new TestHttpMessageHandler
                {
                    Sender = async req =>
                    {
                        var content = await req.Content.ReadAsStringAsync();

                        if (req.RequestUri.AbsoluteUri == "https://www.google.com/recaptcha/api/siteverify")
                        {
                            RecaptchaValidationResponse result;
                            if (content == $"secret=SecretKey&response=Good&remoteip=")
                            {
                                result = new RecaptchaValidationResponse {
                                    Success = true
                                }
                            }
                            ;
                            else if (content == $"secret=SecretKey&response=Bad&remoteip=")
                            {
                                result = new RecaptchaValidationResponse {
                                    Success = false, ErrorCodes = new List <string> {
                                        "invalid-input-response"
                                    }
                                }
                            }
                            ;
                            else
                            {
                                result = new RecaptchaValidationResponse {
                                    Success = false
                                }
                            };

                            var res     = new HttpResponseMessage(HttpStatusCode.OK);
                            var text    = JsonConvert.SerializeObject(result);
                            res.Content = new StringContent(text, Encoding.UTF8, "application/json");
                            return(res);
                        }

                        throw new NotImplementedException(req.RequestUri.AbsoluteUri);
                    }
                };
            });
        }
Example #2
0
        private RecaptchaService CreateTestService(System.Net.HttpStatusCode statusCode, RecaptchaValidationResponse result, string reponse, string ipAddress)
        {
            var options = GetOptions();

            options.Value.BackchannelHttpHandler = new TestHttpMessageHandler
            {
                Sender = async req =>
                {
                    var content = await req.Content.ReadAsStringAsync();

                    Assert.Equal($"secret={_secretKey}&response={reponse}&remoteip={ipAddress}", content);

                    if (req.RequestUri.AbsoluteUri == "https://www.google.com/recaptcha/api/siteverify")
                    {
                        var res  = new HttpResponseMessage(statusCode);
                        var text = JsonConvert.SerializeObject(result);
                        res.Content = new StringContent(text, Encoding.UTF8, "application/json");
                        return(res);
                    }

                    throw new NotImplementedException(req.RequestUri.AbsoluteUri);
                }
            };

            return(new RecaptchaService(options));
        }