예제 #1
0
        /// <summary>
        /// Validate
        /// </summary>
        /// <returns></returns>
        public GReCaptchaResponse Validate()
        {
            GReCaptchaResponse result = null;
            var httpClient            = new HttpClient();
            var requestUri            = string.Empty;

            requestUri = string.Format(RECAPTCHA_VERIFY_URL, SecretKey, Response, RemoteIp);

            try
            {
                var taskResult = httpClient.GetAsync(requestUri);
                taskResult.Wait();
                var response = taskResult.Result;
                response.EnsureSuccessStatusCode();
                var taskString = response.Content.ReadAsStringAsync();
                taskString.Wait();
                result = ParseResponseResult(taskString.Result);
            }
            catch
            {
                result = new GReCaptchaResponse {
                    IsValid = false
                };
                result.ErrorCodes.Add("Unknown error");
            }
            finally
            {
                httpClient.Dispose();
            }

            return(result);
        }
예제 #2
0
        /// <summary>
        /// Parse response
        /// </summary>
        /// <param name="responseString">Response (string)</param>
        /// <returns>Parsed response</returns>
        private GReCaptchaResponse ParseResponseResult(string responseString)
        {
            var result = new GReCaptchaResponse();

            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);
        }