Web API 2中的Google reCaptcha c# [英] Google reCaptcha in Web API 2 c#
本文介绍了Web API 2中的Google reCaptcha c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个ASP.NET Web API 2项目.我正在尝试从表单中读取Google验证码.我尝试了以下代码:
I have an ASP.NET Web API 2 Project. I am trying to read Google Captcha from the form. I tried this Code:
public string Post(FoundingRequest model)
{
var response = Request["g-recaptcha-response"];
string secretKey = "my key";
var client = new WebClient();
var result = client.DownloadString(
$"https://www.google.com/recaptcha/api/siteverify?secret={secretKey}&response={response}");
var obj = JObject.Parse(result);
model.Captcha = (bool)obj.SelectToken("success");
....
}
但是我在第一行收到一个错误:
but I am receiving an Error on the first line:
无法将带有[]的索引应用于类型的表达式'HttpRequestMessage'
Cannot apply indexing with [] to an expression of type 'HttpRequestMessage'
为什么?以及如何解决?谢谢
why? and how to solve it? thank you
推荐答案
我找到了答案,我创建了一个具有特定名称的隐藏文件,并在验证码回调中更新了它的值.代码:
I found the answer, I created a hidden file with a certain name and update its value on Captcha call back. Code:
<input type="hidden" value="" id="recaptcha" name="recaptcha" />
<div class="g-recaptcha" data-callback="imNotARobot" data-sitekey="key"></div>
而Javascript是:
and the Javascript is:
<script type="text/javascript">
var imNotARobot = function () {
$("#recaptcha").val(grecaptcha.getResponse());
};
</script>
服务器端:
public string Recaptcha { get; set; }
,活页夹将完成所有工作.
and the binder does all the work.
这篇关于Web API 2中的Google reCaptcha c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文