Created
October 6, 2025 09:55
-
-
Save uzbekdev1/ded2d03596947805724ba7a7543085eb to your computer and use it in GitHub Desktop.
google recaptcha
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class RecaptchaResponse | |
| { | |
| [JsonProperty("success")] | |
| public bool Success { get; set; } | |
| [JsonProperty("error-codes")] | |
| public List<string> ErrorCodes { get; set; } | |
| } | |
| public class GoogleService | |
| { | |
| private readonly AppSettings _appSettings; | |
| public GoogleService(AppSettings appSettings) | |
| { | |
| _appSettings = appSettings; | |
| } | |
| public async Task<bool> VerifyCaptcha(string token) | |
| { | |
| if (!_appSettings.Config.ReadyProduction) | |
| { | |
| return true; | |
| } | |
| if (string.IsNullOrWhiteSpace(token)) | |
| { | |
| return false; | |
| } | |
| var client = new HttpClient(); | |
| var response = await client.GetAsync($"https://www.google.com/recaptcha/api/siteverify?secret={_appSettings.RecaptchaSecret}&response={token}"); | |
| if (!response.IsSuccessStatusCode) | |
| { | |
| return false; | |
| } | |
| var json = await response.Content.ReadAsStringAsync(); | |
| var result = JsonConvert.DeserializeObject<RecaptchaResponse>(json); | |
| return result.Success; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment