asp.net-mvc 如何在 asp.net mvc 中使用验证码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2286688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Use Captcha in asp.net mvc
提问by mary
can any one tell me how to use captcha in asp.net mvc? is there any need to download any control for it?
谁能告诉我如何在asp.net mvc 中使用验证码?是否需要下载任何控件?
回答by Maxim Zaslavsky
Hoping it's not too late to put my two cents in...
希望现在投入我的两分钱还为时不晚......
Introducing MvcReCaptcha
介绍MvcReCaptcha
I faced this exact same issue when trying to implement CAPTCHA validation on my first ASP.NET MVC site. After discovering many libraries, I found what seemed (and still seems) to be the most straightforward and efficient library: MvcReCaptcha.Since then, I have used this library for all of my ASP.NET MVC sites.
在我的第一个 ASP.NET MVC 站点上尝试实现 CAPTCHA 验证时,我遇到了这个完全相同的问题。在发现了许多库之后,我发现了似乎(现在仍然是)最直接和高效的库:MvcReCaptcha。从那时起,我就将这个库用于我所有的 ASP.NET MVC 站点。
After you implement MvcReCaptcha, it securely generates a CAPTCHA on your view and provides a boolean value of whether the validation was successful to the action.
在您实现MvcReCaptcha 之后,它会在您的视图上安全地生成一个 CAPTCHA,并提供一个布尔值,表明该操作的验证是否成功。
Instructions for Use
使用说明
Here's how to implement it after downloading and referencing the MvcReCaptchaDLL from your project (instructions copied from MvcReCaptchahome page):
以下是从您的项目下载并引用MvcReCaptchaDLL后如何实现它(从MvcReCaptcha主页复制的说明):
Using ReCaptcha with ASP.NET MVC:
It is now extremely easy to setup ReCaptcha on your Asp.Net MVC Website.
Signup for reCaptcha, http://recaptcha.net/whyrecaptcha.html
How to Use:
Step 1: Add your Public and Private key to your web.config file in appsettings section
<appSettings> <add key="ReCaptchaPrivateKey" value=" -- PRIVATE_KEY -- " /> <add key="ReCaptchaPublicKey" value=" -- PUBLIC KEY -- " /> </appSettings>Step 2: Add new namespace to your web.config
<namespaces> <add namespace="MvcReCaptcha.Helpers"/> </namespaces>Step 3: Implement the logic in your view to actually render the Captcha control
<%= Html.GenerateCaptcha() %>Step 4: Implement the Controller Action that will handle the form submission and Captcha validation
[CaptchaValidator] [AcceptVerbs( HttpVerbs.Post )] public ActionResult CreateComment( Int32 id, bool captchaValid ) { if (!captchaValid) { ModelState.AddModelError("_FORM", "You did not type the verification word correctly. Please try again."); } else { // If we got this far, something failed, redisplay form return View(); } }
在 ASP.NET MVC 中使用 ReCaptcha:
现在在您的 Asp.Net MVC 网站上设置 ReCaptcha 非常容易。
注册 reCaptcha,http://recaptcha.net/whyrecaptcha.html
如何使用:
第 1 步:将您的公钥和私钥添加到 appsettings 部分的 web.config 文件中
<appSettings> <add key="ReCaptchaPrivateKey" value=" -- PRIVATE_KEY -- " /> <add key="ReCaptchaPublicKey" value=" -- PUBLIC KEY -- " /> </appSettings>第 2 步:将新命名空间添加到您的 web.config
<namespaces> <add namespace="MvcReCaptcha.Helpers"/> </namespaces>第 3 步:在您的视图中实现逻辑以实际呈现 Captcha 控件
<%= Html.GenerateCaptcha() %>第 4 步:实现将处理表单提交和 Captcha 验证的控制器操作
[CaptchaValidator] [AcceptVerbs( HttpVerbs.Post )] public ActionResult CreateComment( Int32 id, bool captchaValid ) { if (!captchaValid) { ModelState.AddModelError("_FORM", "You did not type the verification word correctly. Please try again."); } else { // If we got this far, something failed, redisplay form return View(); } }
Good luck!
祝你好运!
回答by Fenton
If you don't fancy writing your own Captcha (who does!) you can use a Captcha library, such as this:
如果您不喜欢编写自己的 Captcha(谁写的!),您可以使用 Captcha 库,例如:
http://www.coderjournal.com/2008/03/aspnet-mvc-captcha/
http://www.coderjournal.com/2008/03/aspnet-mvc-captcha/
With a Captcha library, you add the dll to your project and use the Captcha API to display and validate the Captcha image and input.
使用 Captcha 库,您可以将 dll 添加到您的项目中,并使用 Captcha API 来显示和验证 Captcha 图像和输入。
Display a Captcha:
显示验证码:
<label for="captcha">Enter <%= Html.CaptchaImage(50, 180) %> Below</label><br />
<%= Html.TextBox("captcha") %>
And then make sure you add the Captcha attribute to your method:
然后确保将 Captcha 属性添加到您的方法中:
[CaptchaValidation("captcha")]
Recaptcha is just one option when it comes to Captcha's (in fact, it's the option selected by Stack Overflow!)
Recaptcha 只是 Captcha 的一种选择(实际上,它是 Stack Overflow 选择的选项!)

