September 13, 2015 by Anuraj
This is post is about integrating Google reCaptcha for ASP.NET 5. To use reCaptcha in your website, you need to register in the reCaptcha website. You need to provide the label, domain name and email address of the owners.
Once it is completed, you will get a screen like this.
From this screen you can get Site key and Secret key values. Site key used to generate captcha images on client side. For client side integration, include the script reference in the head element.
And based on the theme, paste the HTML code, which renders the captcha.
This is the site key from the dashboard. Now you have completed the client side integration. If you run the page, you can view a captcha image like this.
Now as part of form submission, you need to validate the captcha response. For that you need to post data from your application to Google servers.
Here is the API request details.
- URL: https://www.google.com/recaptcha/api/siteverify
- METHOD: POST
- secret - Required. The shared key between your site and ReCAPTCHA.
- response - Required. The user response token provided by the reCAPTCHA to the user and provided to your site on.
- remoteip - Optional. The user’s IP address.
And here is the code. For registration method.
And here is the request verification code.
The response format is JSON, you can use Newtonsoft.Json.JsonConvert class to deserialize the response. Here is the response class.
Which is generated from the JSON response from documentation.
In this example I am not using the error codes, but you can include that as well if required.
Hope it Helps. Happy Programming :)
Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub