我正在为我的联系表使用 Google ReCaptcha 和 BitArmory NuGet 包。我检查了 Azure 上的日志,发现有些人从 BitArmory ReCaptcha 收到错误响应(siteKey:不能为空??)。
我尝试了多种方式来存储我的站点 key 。开头在我常用的全局变量中。接下来,我在一个函数中创建了一个本地字符串变量并插入到验证码函数中。最后一步是将我的站点 key 字符串硬编码为验证码。在所有情况下结果相同,有些人仍然收到关于 siteKey:can not be null???
的回复
这是我的表单的一部分:
@section head {
<script src="https://www.google.com/recaptcha/api.js?render=xxxxxxx"></script>
}
<form id="contactForm" class="form-horizontal" role="form" method="post" action="/site/comments">
<div class="form-group">
<label for="captcha"></label>
<input id="captcha" class="form-control" type="hidden" name="captcha" value=""/>
</div>
<div class="form-group">
<label for="email" class="col-lg-2 control-label">Email</label>
<div class="col-lg-10">
<input id="email" type="text" class="form-control" value="@ViewBag.Email" name="email" placeholder="me@email.com"/>
</div>
</div>
div class="col-lg-10">
<input type="submit" id="submitBtn" onclick="SubmitClicked()" class="btn btn-primary" value="Send us your feedback!"/>
</div>
<script type="text/javascript">
function SubmitClicked() {
$("#submitBtn").attr('disabled', true);
ExecuteReCaptcha();
}
function ExecuteReCaptcha() {
grecaptcha.ready(function() {
grecaptcha.execute('xxxxxxxx', {action: 'xxxxx'})
.then(function(token) {
// Set `token` in a hidden form input.
$("#captcha").val(token);
// POST Form
postForm();
});
});
}
function postForm() {
$("#contactForm").submit();
还有我的服务器端:
[HttpPost]
public async Task<ActionResult> Comments(string email, string captcha, string regarding, string comment)
{
var clientIp = Request.UserHostAddress;
var token = captcha;
var secret = "xxxxxxxxx";
var captchaApi = new ReCaptchaService();
var results = await captchaApi.Verify3Async(token, clientIp, secret);
if (IsValidEmail(email) && (!results.IsSuccess || results.Score < 0.5 || results.Action != "xxxxxx"))
{
ErrorViewModel eVm = new ErrorViewModel
{
message = "\"" + email + "\" is not a valid email address.",
bShowBackButton = true
};
return View("Error", eVm);
}
一些客户正在收到响应:
System.ArgumentException: The client response must not be null or empty
Parameter name: siteSecret
at BitArmory.ReCaptcha.ReCaptchaService.<Verify3Async>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at BootstrappingMVC.Controllers.SiteController.<Comments>d__37.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.<BeginInvokeAction>b__1c()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)
这与您的 siteSecret
无关。 BitArmory.Recaptcha 代码 ( GitHub ) 中存在错误:
public virtual async Task<ReCaptcha3Response> Verify3Async(string clientToken, string remoteIp, string siteSecret, CancellationToken cancellationToken = default)
{
if( string.IsNullOrWhiteSpace(siteSecret) ) throw new ArgumentException("The secret must not be null or empty", nameof(siteSecret));
if( string.IsNullOrWhiteSpace(clientToken) ) throw new ArgumentException("The client response must not be null or empty", nameof(siteSecret));
这两个 if
语句都会抛出 ArgumentException
,这归咎于 siteSecret
,即使消息提供了正确的原因。在您的情况下,它是空的 token
,而不是 secret
。
至于为什么 token 可以为空,我认为这是因为您没有阻止在 grecaptcha
执行之前提交表单的按钮点击的默认处理。
尝试按如下方式修改您的 SubmitClicked
函数:
function SubmitClicked(e) {
e.preventDefault();
$("#submitBtn").attr('disabled', true);
ExecuteReCaptcha();
}
我是一名优秀的程序员,十分优秀!