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

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

问题描述

谁能解释一下如何在我的 MVC3 应用程序中使用像 stackoverflow 这样的 reCaptcha 功能.

你如何自定义它?

解决方案

我使用 Google ReCaptcha,它运行良好,实施起来也非常简单.

请注意,如果您使用的是 Https,请确保您拥有当前版本的 dll(此时为 1.0.5.0)

您需要在 Google Recaptcha 站点上创建一个帐户并获取一组公钥和私钥.将密钥添加到您的 Web 项目主 web.config 文件中:

<add key="webpages:Version" value="1.0.0.0"/><add key="ClientValidationEnabled" value="true"/><add key="UnobtrusiveJavaScriptEnabled" value="true"/><add key="ReCaptchaPrivateKey" value="把你的私钥值放在这里"/><add key="ReCaptchaPublicKey" value="把你的公钥值放在这里"/></appSettings>

现在使用 NuGet 并为 .NET 安装 reCAPTCHA 插件

然后,转到 VIEWS 文件夹内的 web.config 文件.添加这一行:

<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"/></命名空间>

然后,在您要显示验证码的视图中,在文件顶部添加 using 语句

@using Recaptcha;

然后将其添加到您的视图中:

你是人吗?

<div class="editor-field">@Html.Raw(Html.GenerateCaptcha("captcha", "clean"))@Html.ValidationMessage("验证码")

在您的控制器操作中,您需要修改签名以接受验证码结果:

[HttpPost][RecaptchaControlMvc.CaptchaValidator]public ActionResult ForgotPassword(CheckUsernameViewModel model, bool captchaValid, string captchaErrorMessage) {if (!Membership.EnablePasswordReset)throw new Exception("不允许密码重置
");如果(模型状态.IsValid){如果(验证码有效){return RedirectToAction("AnswerSecurityQuestion", new { username = model.Username });}ModelState.AddModelError("", captchaErrorMessage);}返回视图(模型);}

按照这些步骤,我可以在几个页面上实施验证码,并且运行顺利.请注意,控制器操作上的参数名称必须正确命名:

bool captchaValid, string captchaErrorMessage

如果您更改了这些参数名称,当您的表单回发到控制器操作时,您将在运行时收到错误消息.

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

And how can you customize that?

解决方案

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

Note that if you are using Https be sure you have the current version of the dll (1.0.5.0 at this time)

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>

Now use NuGet and install the reCAPTCHA plugin for .NET

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
");
    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.

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

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