· 6 years ago · May 29, 2019, 10:44 PM
1namespace Concertino.API.Utility
2{
3 public class ReCaptchaUtility : IReCaptchaUtility
4 {
5 private readonly IConfiguration _config;
6 public ReCaptchaUtility(IConfiguration config)
7 {
8 _config = config;
9 }
10
11 public async Task<bool> IsValid(string captcha)
12 {
13 string url = "https://www.google.com/recaptcha/api/siteverify";
14 string secretKey = _config.GetSection("AppSettings:ReCaptchaSecret").Value;
15
16 using (HttpClient http = new HttpClient())
17 {
18 var values = new Dictionary<string, string> { { "secret", secretKey }, { "response", captcha } };
19 var data = new FormUrlEncodedContent(values);
20
21 var postResponse = await http.PostAsync(url, data);
22 var responseString = await postResponse.Content.ReadAsStringAsync();
23 var verifyResponse = JsonConvert.DeserializeObject<ReCaptchaResponseDTO>(responseString);
24
25 return verifyResponse.Success;
26 }
27 }
28 }
29}