PaulMiami / reCAPTCHA

reCAPTCHA 2.0 for ASPNET Core
MIT License
134 stars 37 forks source link

Cannot inject sitekey or secret key from controller for multiple host #32

Closed pkarthik86212 closed 6 years ago

pkarthik86212 commented 6 years ago

Hi,

In my project I having multiple sitekey/secretkey pair for multiple hosts and I am not able to configure the keys from my controller

PaulMiami commented 6 years ago

hi,

This is not supported, but I am pretty sure you can add multiple host name to the same sitekey

pkarthik86212 commented 6 years ago

Thanks for your reply Paul. The problem for me is that an admin can create a new host and while creating a new host they wanted a new site key/secret key for security reasons.

I created my own custom method to validate the keys.

public async Task ValidateRecaptchaAsync(string secretKey) { string recaptchaResponse = this.Request.Form["g-recaptcha-response"];

        if (string.IsNullOrWhiteSpace(recaptchaResponse))
        {
            this.ModelState.AddModelError("captcha", "Please confirm that you are not a robot.");
            return false;
        }

        var httpClient = new HttpClient();
        var httpResponse = await httpClient.GetAsync($"{RecaptchaRequestUrl}?secret={secretKey}&response={recaptchaResponse}");
        if (httpResponse.StatusCode != HttpStatusCode.OK)
        {
            this.ModelState.AddModelError("captcha", "The reCAPTCHA is not valid.");
            return false;
        }

        var jsonResult = httpResponse.Content.ReadAsStringAsync().Result;
        dynamic jsonData = JObject.Parse(jsonResult);
        if (jsonData.success != "true")
        {
            this.ModelState.AddModelError("captcha", "The ReCaptcha is not valid.");
            return false;
        }

        return true;
    }