Last active
August 23, 2023 04:52
-
-
Save A-Programmer/587489be1395dba5be9323255eaaa818 to your computer and use it in GitHub Desktop.
How to use reCaptcha v2 in ASP.NET
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
(Another way : https://github.com/tanveery/recaptcha-net) | |
1. Get secret key and site key from : https://www.google.com/recaptcha/admin | |
2. Add them to web config : | |
<add key="recaptcha-secret-key" value="...[secret key]" /> | |
<add key="recaptcha-public-key" value="...[public-key]" /> | |
3. Install Package : | |
PM > Install-Package ReCaptcha-AspNet | |
4. Optional : | |
You can change language in web.config : (https://developers.google.com/recaptcha/docs/language) | |
<add key="recaptcha-language-key" value="[language-code]" /> | |
Or via C# code : | |
string publicKey = "...[public-key]"; | |
string secretKey = "...[secret-key]"; | |
ReCaptcha.Configure(publicKey, secretKey); | |
// Optional, select a default language: | |
string publicKey = "...[public-key]"; | |
string secretKey = "...[secret-key]"; | |
ReCaptchaLanguage defaultLanguage = ReCaptchaLanguage.German; | |
ReCaptcha.Configure(publicKey, secretKey, defaultLanguage); | |
5. Add this line in <head> tag : | |
<script src='https://www.google.com/recaptcha/api.js'></script> | |
6. and this line (given by google) in bottom of your form : | |
<div class="g-recaptcha" data-sitekey="xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"></div> | |
7. In button click enevn write this code: | |
string userResponse = Request.Params["g-recaptcha-response"]; | |
bool validCaptcha = ReCaptcha.ValidateCaptcha(userResponse); | |
if (validCaptcha) | |
{ | |
// Real User, validated ! | |
Response.Write("Passed"); | |
} | |
else | |
{ | |
// Bot Attack, non validated ! | |
Response.Write("You are robot"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In my case: