Description:
In this
example we explain that how to implement the Google RECaptcha code in
Asp.Net.or how to create Google RECaptcha to validate the user in website in
Asp.Net.or how to implement the I am not Robert Google RECaptcha in Web Application
using Asp.Net.
Google
RECaptcha generally validate the Captcha response on client side using its call
back functions.so how can we implement Google RECaptcha in our website?
Before implement
you have to follow the below link to get the Site key and Secret key of the
Google RECaptcha.
<html>
<head>
<title>Google RECaptcha Code with Example in ASP.Net</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit"
async defer></script>
<script type="text/javascript">
var
onloadCallback = function () {
grecaptcha.render('dv_GoogleRECaptcha', {
'sitekey':
'<%=ReCaptcha_Key
%>',
'callback':
function (response) {
$.ajax({
type: "POST",
url: "Employee.aspx/VerifyCaptcha",
data: "{response: '" + response + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var
captchaResponse = jQuery.parseJSON(r.d);
if (captchaResponse.success) {
$("[id*=txt_GoogleRECaptcha]").val(captchaResponse.success);
$("[id*=rfvCaptcha]").hide();
} else {
$("[id*=txt_GoogleRECaptcha]").val("");
$("[id*=rfvCaptcha]").show();
var error = captchaResponse["error-codes"][0];
$("[id*=rfvCaptcha]").html("RECaptcha error. " + error);
}
}
});
}
});
};
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="dv_GoogleRECaptcha">
</div>
<asp:TextBox ID="txt_GoogleRECaptcha"
runat="server"
Style="display: none" />
<asp:RequiredFieldValidator ID = "rfvCaptcha" ErrorMessage="Captcha validation is required." ControlToValidate="txt_GoogleRECaptcha"
runat="server" ForeColor
= "Red"
Display = "Dynamic" />
<br />
<br />
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />
</form>
</body>
</html>
Aspx.cs:
protected static string
ReCaptcha_Key = "<RECaptcha Site
Key>";
protected
static string
ReCaptcha_Secret = "<RECaptcha Secret
Key>";
[WebMethod]
public static string
VerifyCaptcha(string response)
{
string
url = "https://www.google.com/recaptcha/api/siteverify?secret="
+ ReCaptcha_Secret + "&response="
+ response;
return
(new WebClient()).DownloadString(url);
}
0 comments:
Post a Comment