Skip to content

Instantly share code, notes, and snippets.

@scattered-code
Created March 14, 2022 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scattered-code/31eb88a3c286685ae843259531a582f9 to your computer and use it in GitHub Desktop.
Save scattered-code/31eb88a3c286685ae843259531a582f9 to your computer and use it in GitHub Desktop.
try
{
using (var client = _clientFactory.CreateClient("captcha"))
{
var response = await client.PostAsync("recaptcha/api/siteverify", new FormUrlEncodedContent(new Dictionary<string, string> { { "secret", _captchaSettings.Value.Secret }, { "response", model.Token }, { "remoteip", Request.HttpContext.Connection.RemoteIpAddress?.ToString() } }), ct);
response.EnsureSuccessStatusCode();
var captchaResponse = await response.Content.ReadAsAsync<CaptchaResponse>(ct);
if (!captchaResponse.Success)
{
ModelState.AddModelError(nameof(model.Email), "Invalid captcha");
_logger.LogWarning($"Invalid captcha doing business logic - {model.Email} - {captchaResponse.Hostname}");
return ValidationProblem(ModelState);
}
}
while (!RunningRequests.TryAdd(HttpContext.Connection.RemoteIpAddress.ToString(), true))
await Task.Delay(3000, ct);
await Task.Delay(3000, ct);
//business logic
}
catch (Exception ex)
{
_logger.LogError(ex, "Error doing the business logic");
throw;
}
finally
{
RunningRequests.Remove(HttpContext.Connection.RemoteIpAddress.ToString(), out var _);
}
return Json(results);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment