Hi,
I am using google reCaptcha v2.0, it working fine in locally. but when I update is server then it is not working properly. First time when page is load it is working ok, but if I press f5 button or click on send button , after that it is not working. I get a error. "Blocked a frame with origin "
https://www.google.com" from accessing a cross-origin frame", then I used the following function.
<script type="text/javascript">
var widgetId2;
widgetId2 = grecaptcha.render('renderCptcha', {
'sitekey': 'my_siteKey'
});
grecaptcha.reset(widgetId2);
</script>
Then it is showing two recaptcha check box. I am posing my code bellow...
@using Web
@using Web.Models
@model SendModel
<script src="
https://www.google.com/recaptcha/api.js" async defer></script>
<div id="leftpage">
<h1>@sendSection.Title</h1>
@using (Html.BeginForm("Index", "Send", FormMethod.Post, new { @class = "sendToFriendForm", onsubmit = "javascript:return Checkingspam()" }))
{
@Html.HiddenFor(m => m.CelebrationId)
@Html.HiddenFor(m => m.CelebrationsSectionId)
@Html.Input(Html.TextBoxFor, m => m.Emails)
@Html.Input(Html.TextBoxFor, m => m.Subject)
@Html.Input(Html.TextAreaFor, m => m.Text)
@Html.Input(Html.TextBoxFor, m => m.From)
<div id="renderCptcha"></div>
<div class="g-recaptcha" data-sitekey="my_siteKey"></div>
if (message != null)
{
<div class="send_status" id="status_msg">@Html.Raw(message)</div>
}
<input type="hidden" name="test_email" id="test_email" value="" />
if (@ViewBag.alertMessage != null)
{
<div style="color: rgb(174, 21,0)">@ViewBag.alertMessage </div>
}
<div id="inner1" style="color: red"></div>
<input type="Submit" name="Send" id="checkspam" value="Send" />
}
@Html.Left(node.LeftText)
</div>
<div id="rightpage">
<h2>@sendSection.Title</h2>
<div style="">
@{ Html.RenderPartial("_Collage", new CollageModel(medias)); }
@Html.Right(node.RightText)
</div>
</div>
<script type="text/javascript">
$(document).ready(function hiddenField() {
var x = document.createElement("INPUT");
x.setAttribute("type", "hidden");
x.setAttribute("name", "test_email");
x.setAttribute("id", "test_email");
x.setAttribute("value", "");
var x = document.getElementById("leftpage").appendChild(x);
}
);
function Checkingspam() {
// debugger
var response = document.getElementById("g-recaptcha-response").value;
var value_email = document.getElementById("Emails").value;
var value_subj = document.getElementById("Subject").value;
//alert('response= ' + response);
//alert('value_email= ' + value_email);
//alert('value_subj= ' + value_subj);
if (response != "" && (value_email != "")) {
//document.getElementById('status').innerHTML = "";
// $('#status').show();
return true;
}
else {
if ('@Model.Book.UseCaptcha' == 'True') {
if ((response == "") || (value_email == "") && (value_subj == "")) {
document.getElementById('inner1').innerHTML = "Please complete the reCAPTCHA or fill the textbox values";
//document.getElementById('contacttextbox').innerHTML = "Please fill the textbox values";
return false;
}
}
else
return true;
}
//else {
// document.getElementById('inner1').innerHTML = "Fill the reCAPTCHA or Please give the Correct Mail Id";
// //document.getElementById('inner1').innerHTML = "Fill the reCAPTCHA";
// return false;
//}
// $('#status').show();
}
</script>
@*<script type="text/javascript">
var widgetId2;
widgetId2 = grecaptcha.render('renderCptcha', {
'sitekey': 'my_siteKey'
});
grecaptcha.reset(widgetId2);
</script>*@
-------------And My controller code is-------------------------------
[HttpPost]
public ActionResult Index(string book,SendModel model,string recaptcha_challenge_field,string recaptcha_response_field )
{
const string secret = "my_Secretkey";
var response = Request["g-recaptcha-response"];
var client = new WebClient();
var reply = client.DownloadString(string.Format("
https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", secret, response));
var captchaResponse = JsonConvert.DeserializeObject<SendModel>(reply);
if (!captchaResponse.Success)
{
if (captchaResponse.ErrorCodes.Count <= 0)
{
AlertMessage = AlertMessage + "" + "Incorrect captcha";
ViewBag.alertMessage = AlertMessage;
return View(model);
}
}
return View(model);
}
------------------------------So, Please Please send me the process.. how can I solved this