在mvc 3.0和后者中实现CAPTCHA

时间:2012-03-08 07:30:44

标签: c# asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

我在mvc 4.0应用程序中使用了验证码,并使用了我的参考:Asp.Net MVC CAPTCHA

但它基于mvc 2.0实现它显示图像但在控制器中我没有任何OnPreAction方法我用OnAuctionExcuting替换它但是它没有MethodInfo。如何更新代码以在mvc 3.0或4.0上运行?

4 个答案:

答案 0 :(得分:13)

只需使用Nuget获取Recaptcha包,然后按照以下步骤操作:

http://www.tkglaser.net/2011/10/google-recaptcha-in-aspnet-mvc-3-using.html

答案 1 :(得分:1)

有一个专业编写的库,用于在Microsoft ASP.NET MVC项目中使用验证码,名为Recaptcha for .NET。您可以从Nuget包(Recaptcha for .NET Nuget Package)安装它:

PM>安装包RecaptchaNet

安装完成后,只需按照以下位置的分步说明进行操作:

http://recaptchanet.codeplex.com/documentation

答案 2 :(得分:1)

有一个类似于reCaptcha的验证码控件。你可以用它。真的很棒。 http://supercaptcha.codeplex.com

答案 3 :(得分:1)

Capzcha in Razor MVC4 ASP.Net - C#

Index.cshtml

@model Test.Models.LoginModel 
@{
    ViewBag.Title = "Home Page"; 
 }

 <section id="loginForm"> 
 @using (Html.BeginForm("Login", "Account"))
      {  
     <p><img src='@Url.Action("ShowCaptchaImage","Home")' alt="Case Sensitive"/>   
     </p>  
     <p><span class="requiredField">*   </span>
     <label>Please enter the string as shown above</label></p>            
     <p>@Html.TextBox("CaptchaText")</p>

     <input type="submit" value="Login" />  
     }  
</section>

在上面的代码中,图像源将调用Home Controller

中的ShowCaptchaImage操作

HomeController.cs

public class HomeController : BaseController
{
 public CaptchaImageResult ShowCaptchaImage()
    {
        return new CaptchaImageResult();
    }
}

上面的CaptchaImageResult()将实例化一个图像对象 创建模型

CaptchaImageResult.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace test.Models
{
  public class CaptchaImageResult : ActionResult
  {
    public string GetCaptchaString(int length)
    {
        int intZero = '1';
        int intNine = '9';
        int intA = 'A';
        int intZ = 'Z';
        int intCount = 0;
        int intRandomNumber = 0;
        string strCaptchaString = "";

        Random random = new Random(System.DateTime.Now.Millisecond);

        while (intCount < length)
        {
            intRandomNumber = random.Next(intZero, intZ);
            if (((intRandomNumber >= intZero) && (intRandomNumber <= intNine) || (intRandomNumber >= intA) && (intRandomNumber <= intZ)))
            {
                strCaptchaString = strCaptchaString + (char)intRandomNumber;
                intCount = intCount + 1;
            }
        }
        return strCaptchaString;
    }

    public override void ExecuteResult(ControllerContext context)
    {

        Bitmap bmp = new Bitmap(100, 30);
        Graphics g = Graphics.FromImage(bmp);
        g.Clear(Color.Navy);
        string randomString = GetCaptchaString(6);
        context.HttpContext.Session["captchastring"] = randomString;

        //add noise , if dont want any noisy , then make it false.
        bool noisy = true;
        if (noisy)
        {
            var rand = new Random((int)DateTime.Now.Ticks);
            int i, r, x, y;
            var pen = new Pen(Color.Yellow);
            for (i = 1; i < 10; i++)
            {
                pen.Color = Color.FromArgb(
                (rand.Next(0, 255)),
                (rand.Next(0, 255)),
                (rand.Next(0, 255)));

                r = rand.Next(0, (130 / 3));
                x = rand.Next(0, 130);
                y = rand.Next(0, 30);

                int m = x - r;
                int n = y - r;
                g.DrawEllipse(pen, m, n, r, r);
            }
        }
        //end noise

        g.DrawString(randomString, new Font("Courier", 16), new SolidBrush(Color.WhiteSmoke), 2, 2);
        HttpResponseBase response = context.HttpContext.Response;
        response.ContentType = "image/jpeg";
        bmp.Save(response.OutputStream, ImageFormat.Jpeg);
        bmp.Dispose(); 

    }  

}

}

在LoginModel中

LoginModel.cs

public class LoginModel
  {
    [Required]
    [Display(Name = "Email Address:")]
    public string Email { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password:")]
    public string Password { get; set; }

    public bool IsAuthenticated { get; set; }

    [Required]  
    public string CaptchaText { get; set; }
}