Example #1
0
        public async Task <GoogleReCaptchaResponse> Validate()
        {
            GoogleReCaptchaResponse result = null;
            var httpClient = new HttpClient();
            var requestUri = string.Format(RECAPTCHA_VERIFY_URL, SecretKey, Response, RemoteIp);

            try
            {
                var response = await httpClient.GetAsync(requestUri);

                response.EnsureSuccessStatusCode();
                var contentresult = await response.Content.ReadAsStringAsync();

                result = ParseResponseResult(contentresult);
            }
            catch (Exception exc)
            {
                result = new GoogleReCaptchaResponse {
                    IsValid = false
                };
                result.ErrorCodes.Add("Unknown error" + exc.Message);
            }
            finally
            {
                httpClient.Dispose();
            }

            return(result);
        }
Example #2
0
        private GoogleReCaptchaResponse ParseResponseResult(string responseString)
        {
            var result = new GoogleReCaptchaResponse();

            var resultObject = JObject.Parse(responseString);

            result.IsValid = resultObject.Value <bool>("success");

            if (resultObject.Value <JToken>("error-codes") != null &&
                resultObject.Value <JToken>("error-codes").Values <string>().Any())
            {
                result.ErrorCodes = resultObject.Value <JToken>("error-codes").Values <string>().ToList();
            }

            return(result);
        }