using System; using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Newtonsoft.Json; namespace ServiceHost { //interface public interface IGoogleRecaptcha { Task IsSatisfy(string response); } public class GoogleRecaptcha : IGoogleRecaptcha { private readonly IConfiguration _configuration; private readonly IHttpContextAccessor _accessor; public GoogleRecaptcha(IConfiguration configuration, IHttpContextAccessor accessor) { _configuration = configuration; _accessor = accessor; } public async Task IsSatisfy(string response) { var secretKey = "6Lfhp_AnAAAAANjDDY6DPrbbUQS7k6ZCRmrVP5Lb"; var http = new HttpClient(); var result = await http.PostAsync( $"https://www.google.com/recaptcha/api/siteverify?secret={secretKey}&response={response}", null); if (result.IsSuccessStatusCode) { var googleResponse = JsonConvert.DeserializeObject(await result.Content.ReadAsStringAsync()); if (googleResponse == null) return false; return googleResponse.Success; } return false; } } public class RecaptchaResponse { [JsonProperty("success")] public bool Success { get; set; } [JsonProperty("challenge_ts")] public DateTimeOffset ChallengeTs { get; set; } [JsonProperty("hostname")] public string HostName { get; set; } } }