0

我在我的 ASP.NET MVC 项目中使用 google ReCAPTCHA,现在我想计算一个人输入错误单词的次数或他尝试了多少次

请帮我

4

1 回答 1

1

一个简单的实现是将值存储在 cookie 中。

(cshtml/剃须刀)

<h2>@ViewBag.Captcha</h2>
@using (Html.BeginForm("Captcha", "Home", FormMethod.Post))
{    
    @Microsoft.Web.Helpers.ReCaptcha.GetHtml(publicKey: "...")    
    <input type="submit" value="validate"/>
}

(控制器)

public ActionResult Index()
{
    ViewBag.Captcha = 0;
    return View();
}


public ActionResult Captcha()
    {
        var valid = Microsoft.Web.Helpers.ReCaptcha.Validate(privateKey: "...");
        if (valid)
        {
            return RedirectToAction("Success");
        }
        else
        {
            int failValue = 0;
            var failCookie = Request.Cookies["failCount"];

            if (failCookie == null)
            {
                failValue = 1;
                failCookie = new HttpCookie("failCount");
                failCookie.Value = failValue.ToString();
                Response.Cookies.Add(failCookie);
            }
            else
            {
                failValue = int.Parse(failCookie.Value);
                failValue = failValue + 1;
                failCookie.Value = failValue.ToString();
            }

            ViewBag.Captcha = "Failed " + failValue + " times";
            return View("Index");
        }           
    }
于 2013-02-28T14:56:35.013 回答