public static Task <bool> ValidateCaptcha(string recaptchaResponse) { #if !DEBUG if (string.IsNullOrEmpty(recaptchaResponse)) { return(Task.FromResult(false)); } return(ReCaptcha.ValidateCaptchaAsync(recaptchaResponse)); #else return(Task.FromResult(true)); #endif }
public async Task <ActionResult> Contact(EmailMessage emailMessage, FormCollection form) { _logger.DebugFormat("'{0}.{1}' called", GetType().Name, nameof(Contact)); ViewBag.Message = "Message Failed"; string userResponse = HttpContext.Request.Params["g-recaptcha-response"]; if (!await ReCaptcha.ValidateCaptchaAsync(userResponse)) { ViewBag.Message = "Please Try again. Captcha not valid"; _logger.WarnFormat("Captia was not valid: {0}", userResponse); // Bot Attack, non validated ! return(View()); } if (ModelState.IsValid) { if (!string.IsNullOrEmpty(emailMessage.FromAddress) && !string.IsNullOrEmpty(emailMessage.MessageContent)) { // Get the secret key value from string appKey = Environment.GetEnvironmentVariable("AGameFunctionAppKey"); emailMessage.Subject = "Contact Form: mbcarey.com"; RestClient client = new RestClient(Settings.Default.AGameWebUrl); RestRequest request = new RestRequest($"api/PersonalEmail?code={appKey}", Method.POST, DataFormat.Json); request.AddJsonBody(await emailMessage.ToJsonAsync()); IRestResponse response = await client.ExecuteAsync(request); if (response.StatusCode == HttpStatusCode.OK) { ViewBag.Message = "Message Sent"; } } } ModelState.Clear(); return(View()); }
public void AssertTestWillConectAndFailInvalidUserAnswerAsyncWithProxy() { Assert.Throws <ReCaptchaException>(() => { try { ReCaptcha.Configure(SiteKey, SecretKey); var task = ReCaptcha.ValidateCaptchaAsync("resposta-fajuta", new WebProxy(TestProxyIp, PortProxy)); while (task.IsCompleted == false) { Thread.Sleep(1); } var answer = task.Result; Assert.IsFalse(answer); } catch (AggregateException e) { throw e.InnerException; } }); }