Reputation: 2676
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 ?
Upvotes: 9
Views: 14851
Reputation: 197
Captcha in Razor MVC4 ASP.Net - C#
@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
public class HomeController : BaseController
{
public CaptchaImageResult ShowCaptchaImage()
{
return new CaptchaImageResult();
}
}
The above CaptchaImageResult() will instantiate a image object Create a model
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
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; }
}
Upvotes: 1
Reputation: 11
There is a captcha control similar to reCaptcha. You can use that. It is really good. http://supercaptcha.codeplex.com
Upvotes: 1
Reputation: 11
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
Upvotes: 1
Reputation: 93424
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
Upvotes: 13