using System; using System.Text.Json.Serialization; using System.Threading.Tasks; using Flurl.Http; using Sozsoft.Platform.Localization; using Microsoft.Extensions.Localization; using Volo.Abp; using Volo.Abp.ObjectMapping; using Volo.Abp.Settings; using Volo.Abp.Users; namespace Sozsoft.Platform.Identity; public class TurnstileCaptchaManager : PlatformDomainService, ICaptchaManager { public TurnstileCaptchaManager( ISettingProvider settingProvider, IStringLocalizer localizer, ICurrentUser currentUser, IObjectMapper objectMapper) : base(settingProvider, localizer, currentUser, objectMapper, null) { } public async Task VerifyCaptchaAsync(string CaptchaResponse, bool throwOnFailure = false) { if (CaptchaResponse.IsNullOrWhiteSpace()) { return false; } var endPoint = await SettingProvider.GetOrNullAsync(PlatformConsts.AbpAccount.Captcha.EndPoint); var privateKey = await SettingProvider.GetOrNullAsync(PlatformConsts.AbpAccount.Captcha.SecretKey); if (endPoint.IsNullOrWhiteSpace() || privateKey.IsNullOrWhiteSpace()) { //alertManager.Alerts.Danger(LK[PlatformConsts.AbpIdentity.User.CaptchaWrongCode]); return false; } // METHOD: POST /* * secret=verysecret * response= * remoteip=ip (Optional) */ var response = await endPoint.PostUrlEncodedAsync(new { secret = privateKey, response = CaptchaResponse }); if (response != null && response.StatusCode == 200) { var result = await response.GetJsonAsync(); if (result?.Success != true) { if (throwOnFailure) { throw new UserFriendlyException(Localizer[PlatformConsts.AbpIdentity.User.CaptchaWrongCode]); } } return result?.Success == true; } return false; } private class TurnstileCaptchaVerifyResponse { [JsonPropertyName("success")] public bool Success { get; set; } } }