Beispiel #1
0
        public GoogleRecaptchaService(IOptions <AppOptions> optionsAccessor)
        {
            _httpClient             = new HttpClient();
            _httpClient.BaseAddress = new Uri("https://www.google.com");

            _options = optionsAccessor.Value.ReCaptcha;
        }
Beispiel #2
0
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            ReCaptchaOptions reCaptchaOptions = context.HttpContext.RequestServices.GetRequiredService <IOptions <ReCaptchaOptions> >().Value;

            if (reCaptchaOptions.UseReCaptcha)
            {
                IReCaptchaRequest reCaptchaRequest = null;
                foreach (KeyValuePair <string, object> argument in context.ActionArguments)
                {
                    if (argument.Value is IReCaptchaRequest)
                    {
                        reCaptchaRequest = (IReCaptchaRequest)argument.Value;
                        break;
                    }
                }

                if (reCaptchaRequest == null)
                {
                    throw new Exception("recaptcha_token_not_foaund");
                }

                ReCaptchaTokentValidationService reCaptchaTokentValidationService =
                    context.HttpContext.RequestServices.GetRequiredService <ReCaptchaTokentValidationService>();

                Result result = await reCaptchaTokentValidationService.Validate(reCaptchaRequest.ReCaptchaToken, _requiredScore, _action);

                if (result.Failure)
                {
                    context.ModelState.AddResultErrors(result);
                }
            }

            await next.Invoke();
        }
        public async Task <bool> ValidateAsync(HttpContext httpContext, Form form, Field field, string value)
        {
            ReCaptchaOptions options = httpContext.RequestServices.GetService <IOptions <ReCaptchaOptions> >()?.Value;

            if (options == null)
            {
                return(false);
            }

            string url = $"https://www.google.com/recaptcha/api/siteverify?secret={options.Secret}&response={value}";

            using (HttpClient httpClient = new HttpClient())
            {
                try
                {
                    string responseString = await httpClient.GetStringAsync(url);

                    return(JsonConvert.DeserializeObject <VerificationResult>(responseString).Success);
                }

                catch
                {
                    return(false);
                }
            }
        }
Beispiel #4
0
        public GoogleRecaptchaRepository(IOptions <AppOptions> optionsAccessor)
        {
            _httpClient = new HttpClient
            {
                BaseAddress = new Uri("https://www.google.com")
            };

            _options = optionsAccessor.Value.ReCaptcha;
        }
        public ReCaptchaTokentValidationService(
            IHttpClientFactory httpClientFactory,
            IOptions <ReCaptchaOptions> reCaptchaOptions,
            ILogger <ReCaptchaTokentValidationService> logger)
        {
            _httpClientFactory = httpClientFactory;

            _reCaptchaOptions = reCaptchaOptions.Value;

            _logger = logger;
        }
        public static void AddReCaptcha(this IServiceCollection services, Action <ReCaptchaOptions> options)
        {
            services.AddScoped <ReCaptchaOptions>(factory =>
            {
                var opt = new ReCaptchaOptions();

                options(opt);

                return(opt);
            });

            services.AddHttpClient(GoogleHttpClientName.Name, (serviceProvider, client) =>
            {
                using (var scope = serviceProvider.CreateScope())
                {
                    var recaptchaOptions = (ReCaptchaOptions)scope.ServiceProvider.GetService(typeof(ReCaptchaOptions));

                    client.BaseAddress = new Uri("https://www.google.com/recaptcha/api/siteverify");
                }
            });

            services.AddScoped <IReCaptchaService, ReCaptchaService>();
        }
Beispiel #7
0
 public RecaptchaService(IOptions <ReCaptchaOptions> optionsAccessor)
 {
     _options = optionsAccessor?.Value ?? new ReCaptchaOptions();
 }