public async Task <GReCaptchaResponse> Validate() { GReCaptchaResponse 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 GReCaptchaResponse { IsValid = false }; result.ErrorCodes.Add("Unknown error" + exc.Message); } finally { httpClient.Dispose(); } return(result); }
private GReCaptchaResponse ParseResponseResult(string responseString) { var result = new GReCaptchaResponse(); if (_version == ReCaptchaVersion.Version1) { var resultObject = responseString.Split('\n'); result.IsValid = resultObject.Contains("true"); if (!result.IsValid) { result.ErrorCodes.AddRange(resultObject.Where(r => !r.Equals("false", StringComparison.OrdinalIgnoreCase))); } } else if (_version == ReCaptchaVersion.Version2) { 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); }
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); }
public GReCaptchaResponse Validate() { GReCaptchaResponse result = null; var httpClient = new HttpClient(); var requestUri = string.Empty; switch (_version) { case ReCaptchaVersion.Version2: requestUri = string.Format(RECAPTCHA_VERIFY_URL_VERSION2, SecretKey, Response, RemoteIp); break; default: requestUri = string.Format(RECAPTCHA_VERIFY_URL_VERSION2, SecretKey, Response, RemoteIp); break; } 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 (Exception exc) { result = new GReCaptchaResponse { IsValid = false }; result.ErrorCodes.Add("Unknown error" + exc.Message); } finally { httpClient.Dispose(); } return(result); }