9

I used captcha in mvc 4.0 application and my reference : Asp.Net MVC CAPTCHA

but it implements base on mvc 2.0 it shows an image but in the controller I don't have any OnPreAction method I replace it with OnAuctionExcuting but there is no MethodInfo in it. how I can update code to run on mvc 3.0 or 4.0 ?

1

4 Answers 4

13

Just use Nuget to get the Recaptcha package, then follow this:

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

2
  • I dont want razor engine I want only the captcha on above link if you look at again to my question thanks. I need just convert it to mvc 3.0 or latter.
    – kamiar3001
    Mar 12, 2012 at 22:18
  • @kamiar3001 - there's nothing Razor specifc about it, just do the same thing in webforms. Mar 12, 2012 at 22:25
1

There is a professionally written library for using captcha in Microsoft ASP.NET MVC project called Recaptcha for .NET. You can install it from the Nuget package (Recaptcha for .NET Nuget Package):

PM> Install Package RecaptchaNet

Once installed, just follow the step-by-step instructions from the following location:

http://recaptchanet.codeplex.com/documentation

1

There is a captcha control similar to reCaptcha. You can use that. It is really good. http://supercaptcha.codeplex.com

1

Captcha 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>

In the above code, the image source will call the ShowCaptchaImage action in Home controller

HomeController.cs

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

The above CaptchaImageResult() will instantiate a image object Create a model

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(); 

    }  

}

}

In 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; }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.