Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build RecaptchaClient on demand. #14335

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
Expand All @@ -14,16 +15,14 @@ namespace OrchardCore.ReCaptcha.Services
{
public class ReCaptchaService
{
private readonly ReCaptchaClient _reCaptchaClient;
private readonly ReCaptchaSettings _settings;
private readonly IEnumerable<IDetectRobots> _robotDetectors;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ILogger _logger;
protected readonly IStringLocalizer S;

public ReCaptchaService(ReCaptchaClient reCaptchaClient, IOptions<ReCaptchaSettings> optionsAccessor, IEnumerable<IDetectRobots> robotDetectors, IHttpContextAccessor httpContextAccessor, ILogger<ReCaptchaService> logger, IStringLocalizer<ReCaptchaService> stringLocalizer)
public ReCaptchaService(IOptions<ReCaptchaSettings> optionsAccessor, IEnumerable<IDetectRobots> robotDetectors, IHttpContextAccessor httpContextAccessor, ILogger<ReCaptchaService> logger, IStringLocalizer<ReCaptchaService> stringLocalizer)
{
_reCaptchaClient = reCaptchaClient;
_settings = optionsAccessor.Value;
_robotDetectors = robotDetectors;
_httpContextAccessor = httpContextAccessor;
Expand Down Expand Up @@ -65,7 +64,8 @@ public void ThisIsAHuman()
/// <returns></returns>
public async Task<bool> VerifyCaptchaResponseAsync(string reCaptchaResponse)
{
return !string.IsNullOrWhiteSpace(reCaptchaResponse) && await _reCaptchaClient.VerifyAsync(reCaptchaResponse, _settings.SecretKey);
var client = _httpContextAccessor.HttpContext.RequestServices.GetRequiredService<ReCaptchaClient>();
jtkech marked this conversation as resolved.
Show resolved Hide resolved
return !string.IsNullOrWhiteSpace(reCaptchaResponse) && await client.VerifyAsync(reCaptchaResponse, _settings.SecretKey);
}

/// <summary>
Expand Down