Created
March 14, 2022 18:10
-
-
Save scattered-code/31eb88a3c286685ae843259531a582f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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