DocSpace-buildtools/web/ASC.Web.Core/Recaptcha.cs

73 lines
2.0 KiB
C#
Raw Normal View History

2021-09-17 14:32:37 +00:00
using System;
using System.IO;
2021-10-25 11:03:14 +00:00
using System.Net.Http;
2021-09-17 14:32:37 +00:00
using System.Security.Authentication;
2021-10-25 11:03:14 +00:00
using System.Text;
2021-09-17 14:32:37 +00:00
2021-09-17 15:18:34 +00:00
using ASC.Common;
2021-09-17 14:32:37 +00:00
using ASC.Web.Studio.Core;
using Newtonsoft.Json.Linq;
namespace ASC.Web.Core
{
public class RecaptchaException : InvalidCredentialException
{
public RecaptchaException()
{
}
public RecaptchaException(string message)
: base(message)
{
}
}
2021-09-17 15:18:34 +00:00
[Scope]
2021-09-17 14:32:37 +00:00
public class Recaptcha
{
private SetupInfo SetupInfo { get; }
public Recaptcha(SetupInfo setupInfo)
{
SetupInfo = setupInfo;
}
public bool ValidateRecaptcha(string response, string ip)
{
try
{
var data = string.Format("secret={0}&remoteip={1}&response={2}", SetupInfo.RecaptchaPrivateKey, ip, response);
2021-10-25 11:03:14 +00:00
var request = new HttpRequestMessage();
request.RequestUri = new Uri(SetupInfo.RecaptchaVerifyUrl);
request.Method = HttpMethod.Post;
request.Content = new StringContent(data, Encoding.UTF8, "application/x-www-form-urlencoded");
2021-09-17 14:32:37 +00:00
2021-10-25 11:03:14 +00:00
using var httpClient = new HttpClient();
2021-11-24 19:34:39 +00:00
using var httpClientResponse = httpClient.Send(request);
using (var reader = new StreamReader(httpClientResponse.Content.ReadAsStream()))
2021-09-17 14:32:37 +00:00
{
var resp = reader.ReadToEnd();
var resObj = JObject.Parse(resp);
if (resObj["success"] != null && resObj.Value<bool>("success"))
{
return true;
}
if (resObj["error-codes"] != null && resObj["error-codes"].HasValues)
{
return false;
}
}
}
catch (Exception)
{
}
return false;
}
}
}