Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

CaptchaHttpClient.cs

using Microsoft.Net.Http.Headers;
using Newtonsoft.Json;
using Nop.Core;
using Nop.Core.Domain.Security;
using Nop.Services.Security;

namespace Nop.Web.Framework.Security.Captcha;

/// 
/// Represents the HTTP client to request reCAPTCHA service
/// 
public partial class CaptchaHttpClient
{
    #region Fields

    protected readonly CaptchaSettings _captchaSettings;
    protected readonly HttpClient _httpClient;
    protected readonly IWebHelper _webHelper;

    #endregion

    #region Ctor

    public CaptchaHttpClient(CaptchaSettings captchaSettings,
        HttpClient client,
        IWebHelper webHelper)
    {
        _captchaSettings = captchaSettings;
        _httpClient = client;
        _webHelper = webHelper;

        //configure client
        client.BaseAddress = new Uri(captchaSettings.ReCaptchaApiUrl);
        client.DefaultRequestHeaders.Add(HeaderNames.UserAgent, $"nopCommerce-{NopVersion.CURRENT_VERSION}");

        if (captchaSettings.ReCaptchaRequestTimeout is int timeout && timeout > 0)
            client.Timeout = TimeSpan.FromSeconds(timeout);
    }

    #endregion

    #region Methods

    /// 
    /// Validate reCAPTCHA
    /// 
    /// Response value
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the asynchronous task whose result contains response from the reCAPTCHA service
    /// 
    public virtual async Task ValidateCaptchaAsync(string responseValue)
    {
        //prepare URL to request
        var url = string.Format(NopSecurityDefaults.RecaptchaValidationPath,
            _captchaSettings.ReCaptchaPrivateKey,
            responseValue,
            _webHelper.GetCurrentIpAddress());

        //get response
        var response = await _httpClient.GetStringAsync(url);
        return JsonConvert.DeserializeObject(response);

    }

    #endregion
}