// POST api/values public async Task <HttpResponseMessage> PostAsync() { var contentAsString = await Request.Content.ReadAsStringAsync(); var response = new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent("Recaptcha response is invalid") }; var parameters = contentAsString.Split('&'); var gRecaptchaResponse = parameters.FirstOrDefault(p => p.StartsWith("g-recaptcha-response=")); gRecaptchaResponse = gRecaptchaResponse?.Replace("g-recaptcha-response=", string.Empty); var googleRechaptchaRequest = new RechaptchaRequest { Response = gRecaptchaResponse, RemoteIp = Request.GetOwinContext().Request.RemoteIpAddress }; var isCorrect = await _recaptchaManager.VerifyAsync(googleRechaptchaRequest); if (isCorrect) { response.StatusCode = HttpStatusCode.OK; response.Content = null; Console.WriteLine("Response = {0} is correct", googleRechaptchaRequest.Response); } else { Console.WriteLine("Response = {0} is correct", googleRechaptchaRequest.Response); } return(response); }
public async Task <bool> VerifyAsync(RechaptchaRequest request) { using (_httpClient) { var parameters = new[] { new KeyValuePair <string, string>("secret", _secretApi), new KeyValuePair <string, string>("response", request.Response), new KeyValuePair <string, string>("remoteip", request.RemoteIp), }; var content = new FormUrlEncodedContent(parameters); var result = await _httpClient.PostAsync(SITE_VERIFY_URL, content); var resultContent = await result.Content.ReadAsStringAsync(); var googleResponse = JsonConvert.DeserializeObject <GoogleRecaptchaReponse>(resultContent); if (!googleResponse.Success) { Console.WriteLine("Errors: {0}", String.Join(@"\n", googleResponse.ErrorCodes)); } return(googleResponse.Success); } }