reCaptcha验证失败

时间:2018-08-23 14:35:25

标签: c# recaptcha

我有一个带有联系我们表格的简单MVC应用程序。

我已经在表单中添加了reCaptcha检查。

<script src='https://www.google.com/recaptcha/api.js'></script>

<div class="g-recaptcha" data-sitekey="6LffV2sUAAAAACP_0dX3HEUfnIhl3ilrxq6p0oYF"></div>

以及控制器动作:

 [HttpPost]
    public ActionResult ContactUs(ContactUsModel contactUsModel)
    {
        try
        {
            if (ModelState.IsValid)
            {
                var s = ConfigurationManager.AppSettings["ReCaptcha.PrivateKey"];
                if(ReCaptcha.Validate(ConfigurationManager.AppSettings["ReCaptcha.PrivateKey"])) { 
                var message = new StringBuilder();
                message.Append("Name: " + contactUsModel.Name + "<br>");
                message.Append("Email: " + contactUsModel.Email + "<br>");
                message.Append("Message: " + contactUsModel.Message);

                Email e = new Email
                {
                    To = "test@test.com",
                    Subject = "Contact Us request",
                    From = "no-reply@test.com",
                    Body = message.ToString()
                };

                var tEmail = new Thread(() =>
                    SendEmail(e));
                tEmail.Start();
                return RedirectToAction("index");
                }
            }
            ViewBag.RecaptchaLastErrors = ReCaptcha.GetLastErrors(this.HttpContext);

            ViewBag.publicKey = ConfigurationManager.AppSettings["ReCaptcha.PublicKey"];

            return View(contactUsModel);
        }
        catch (Exception e)
        {
            return RedirectToAction("index");
        }
}

但是if (ModelState.IsValid)出错了,在catch中,我得到了:

  

{“基础连接已关闭:发生意外错误   正在发送。“}

什么原因导致错误?

我在web.config中设置了密钥:

<add key="ReCaptcha.PrivateKey" value="xxxxxxxxx" />
<add key="ReCaptcha.PublicKey" value="yyyyyyyyyy" />

0 个答案:

没有答案