Cya*_*sin 11 html ajax asp.net-mvc jquery recaptcha
我正在使用ASP.Net MVC并尝试将Google reCaptcha对象实现到页面中.
我试图避免在我的表单中使用模型,并希望直接使用jquery ajax调用方法.
我已经有了验证码,但是在调试器中检查RecaptchaVerificationHelper对象时,无论我输入什么都显示为null.
任何建议,以保持它轻量级,就像我拥有它,但保持它的工作.
注意:大多数逻辑都已在这里被删除,只是试图让验证码逻辑工作.
CSHTML示例:
@using Recaptcha.Web.Mvc;
<script type="text/javascript">
function createUser() {
$.ajax({
type: "POST",
url: 'CreateUser',
contentType: "application/json; charset=utf-8",
success: function (response) {
if (response.Success == true) {
alert("success");
//redirectSuccess();
} else {
alert("failed");
}
},
error: function (err) {
commonError(err);
}
});
}
</script>
@Html.Recaptcha(publicKey: "6LdxcPgSAA...", theme: Recaptcha.Web.RecaptchaTheme.Clean);
<br />
<input type="button" value="Submit" onclick="createUser();" style="margin-right:300px;" />
Run Code Online (Sandbox Code Playgroud)
CS服务器代码示例:
public ActionResult User()
{
return View();
}
public JsonResult CreateUser()
{
Wrapper.ValidationResponse response = new Wrapper.ValidationResponse();
response.Success = true;
RecaptchaVerificationHelper recaptchaHelper = this.GetRecaptchaVerificationHelper();
if (String.IsNullOrEmpty(recaptchaHelper.Response))
{
response.Success = false;
}
RecaptchaVerificationResult recaptchaResult = recaptchaHelper.VerifyRecaptchaResponse();
if (recaptchaResult != RecaptchaVerificationResult.Success)
{
response.Success = false;
}
try
{
//removed logic
return Json(response);
}
catch (Exception ex)
{
response.Success = false;
response.Message = "Failed to create new user. Please contact us if the issue persists.";
return Json(response);
}
}
Run Code Online (Sandbox Code Playgroud)
提前致谢,
Cya*_*sin 11
在疯了一个多星期后,我终于使用开发人员API直接获得了一个有效的解决方案.
我所做的是使用jsAPI并使用API手动将captcha控件添加到页面.在提交时,我捕获了recaptcha响应并将其发送到服务器端.
然后,从服务器端,我根据API指令验证了请求,并使用此处的教程:http://www.codeproject.com/Tips/851004/How-to-Validate-Recaptcha-V-Server-side 然后我发送了请求并相应地处理响应.
HTML:
<script type="text/javascript"
src='https://www.google.com/recaptcha/api.js'></script>
<script type="text/javascript">
var captcharesponse = grecaptcha.getResponse();
$.ajax({
type: "POST",
url: 'CreateUser',
data: "{captcharesponse:" + JSON.stringify(captcharesponse) + "}",
contentType: "application/json; charset=utf-8",
success: function (response) {
if (response.Success == true) {
alert("success");
} else {
alert("failed");
}
},
error: function (err) {
commonError(err);
}
});
}
</script>
<div class="g-recaptcha"
data-sitekey="[public key here]"></div>
<br />
<input type="button" value="Submit" onclick="createUser();" style="margin-right:300px;" />
Run Code Online (Sandbox Code Playgroud)
CS:
[HttpPost]
public JsonResult CreateUser(string captcharesponse)
{
Wrapper.ValidationResponse response = new Wrapper.ValidationResponse();
response.Success = true;
if (Recaptcha.Validate.Check(captcharesponse) == false)
{
response.Success = false;
}
try
{
//removed logic
return Json(response);
}
catch (Exception ex)
{
response.Success = false;
response.Message = "Failed to create new user. Please contact us if the issue persists.";
return Json(response);
}
}
public class Validate
{
public static bool Check(string response)
{
//string Response = HttpContext.Current.Request.QueryString["g-recaptcha-response"];//Getting Response String Append to Post Method
bool Valid = false;
//Request to Google Server
HttpWebRequest req = (HttpWebRequest)WebRequest.Create
(" https://www.google.com/recaptcha/api/siteverify?secret=" + WebConfigurationManager.AppSettings["recaptchaPrivateKey"] + "&response=" + response);
try
{
//Google recaptcha Response
using (WebResponse wResponse = req.GetResponse())
{
using (StreamReader readStream = new StreamReader(wResponse.GetResponseStream()))
{
string jsonResponse = readStream.ReadToEnd();
JavaScriptSerializer js = new JavaScriptSerializer();
MyObject data = js.Deserialize<MyObject>(jsonResponse);// Deserialize Json
Valid = Convert.ToBoolean(data.success);
}
}
return Valid;
}
catch (WebException ex)
{
throw ex;
}
}
}
public class MyObject
{
public string success { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6549 次 |
| 最近记录: |