如何实现谷歌的reCAPTCHA在MVC3应用程序? [英] How to implement Google reCaptcha in an MVC3 application?

查看:113
本文介绍了如何实现谷歌的reCAPTCHA在MVC3应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释如何有验证码的功能就像在我MVC3应用计算器。

Can anyone explain how to have reCaptcha functionality like stackoverflow in my MVC3 application.

你怎么可以自定义?

推荐答案

我用的是谷歌验证码,它工作得很好,是很容易实现。

I use the Google ReCaptcha and it works very well and is very simple to implement.

请注意,如果您使用的是HTTPS确保您有(此时1.0.5.0)当前版本的DLL

您需要创建在谷歌的Recaptcha网站的帐户,并得到一组公钥和私钥的。键添加到您的Web项目的主要web.config文件:

You need to create an account on the Google Recaptcha site and get a set of public and private keys. Add the keys to your web project main web.config file:

<appSettings>
    <add key="webpages:Version" value="1.0.0.0"/>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
    <add key="ReCaptchaPrivateKey" value="put your private key value here" />
    <add key="ReCaptchaPublicKey" value="put your public key value here" />
</appSettings>

现在使用的NuGet并安装验证码插件.NET

Now use NuGet and install the reCAPTCHA plugin for .NET

然后,转到你的web.config文件的意见文件夹内。加入这一行:

Then, go to your web.config file inside of your VIEWS folder. Add this line:

<namespaces>
  <add namespace="System.Web.Mvc" />
  <add namespace="System.Web.Mvc.Ajax" />
  <add namespace="System.Web.Mvc.Html" />
  <add namespace="System.Web.Routing" />
  <add namespace="Recaptcha"/>
</namespaces>

然后,在你想显示验证码你的看法,你的文件的顶部

Then, in your view that you want to show the captcha, add the using statement at the top of your file

@using Recaptcha;

然后添加到您的看法:

then add this to your view:

<div class="editor-label">
    Are you a human?
</div>
<div class="editor-field">
    @Html.Raw(Html.GenerateCaptcha("captcha", "clean"))
    @Html.ValidationMessage("captcha")
</div>

在你的控制器动作,你将需要修改签名就可以接受验证码的结果:

In your controller action you will need to modify the signature to accept the captcha results:

[HttpPost]
[RecaptchaControlMvc.CaptchaValidator]
public ActionResult ForgotPassword(CheckUsernameViewModel model, bool captchaValid, string captchaErrorMessage) {
    if (!Membership.EnablePasswordReset)
        throw new Exception("Password reset is not allowed\r\n");
    if(ModelState.IsValid) {
        if(captchaValid) {
            return RedirectToAction("AnswerSecurityQuestion", new { username = model.Username });
        }
        ModelState.AddModelError("", captchaErrorMessage);
    }
    return View(model);
}

经过这些步骤都让我实施几页验证码和它的作品顺利。请注意,在控制器动作中的参数名称必须正确命名为:

Following those steps have allowed me to implement captcha on several pages and it works smoothly. Note that the parameter names on the controller action MUST BE NAMED CORRECTLY:

bool captchaValid, string captchaErrorMessage

如果您改变了这些参数的名称,你会在运行时得到一个错误,当你的形式回发到控制器动作。

If you changed these parameter names you WILL get an error at runtime when your form posts back to the controller action.

这篇关于如何实现谷歌的reCAPTCHA在MVC3应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆