75 lines
2.2 KiB
C#
75 lines
2.2 KiB
C#
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<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;
|
|
}
|
|
|
|
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)
|
|
{
|
|
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; }
|
|
}
|
|
}
|