sozsoft-platform/api/src/Sozsoft.Platform.Domain/Identity/TurnstileCaptchaManager.cs

76 lines
2.2 KiB
C#
Raw Normal View History

using System;
using System.Text.Json.Serialization;
2026-02-24 20:44:16 +00:00
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<PlatformResource> localizer,
ICurrentUser currentUser,
IObjectMapper objectMapper)
: base(settingProvider, localizer, currentUser, objectMapper, null)
{
}
public async Task<bool> VerifyCaptchaAsync(string CaptchaResponse, bool throwOnFailure = false)
{
if (CaptchaResponse.IsNullOrWhiteSpace())
{
return false;
}
2026-02-24 20:44:16 +00:00
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=<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<TurnstileCaptchaVerifyResponse>();
if (result?.Success != true)
2026-02-24 20:44:16 +00:00
{
if (throwOnFailure)
{
throw new UserFriendlyException(Localizer[PlatformConsts.AbpIdentity.User.CaptchaWrongCode]);
}
}
return result?.Success == true;
2026-02-24 20:44:16 +00:00
}
return false;
}
private class TurnstileCaptchaVerifyResponse
{
[JsonPropertyName("success")]
public bool Success { get; set; }
}
}