Captcha Code In MVC C#
This
article will show how you can create a custom captcha code in your mvc
application using c#.net.
using System;
using
System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace
MVC_Demos.Controllers
{
public class CaptchaController : Controller
{
//
// GET: /Captcha/
public ActionResult
CaptchaIndex()
{
string[] strArray = new string[36];
strArray =
new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
Random autoRand = new Random();
string strCaptcha = string.Empty;
for (int i = 0; i < 6; i++)
{
int j = Convert
.ToInt32(autoRand.Next(0,
62));
strCaptcha += strArray[j].ToString();
}
Session["Captcha"] = strCaptcha;
ImageConverter
converter =
new ImageConverter();
Response.BinaryWrite((
byte[])converter.ConvertTo(CaptchaGeneration(strCaptcha), typeof(byte[])));
return View();
}
public Bitmap
CaptchaGeneration(
string captchatxt)
{
Bitmap bmp = new Bitmap(133, 48);
using (Graphics graphics = Graphics.FromImage(bmp))
{
Font font = new Font("Tahoma", 14);
graphics.FillRectangle(new SolidBrush(Color.Gray), 0, 0, bmp.Width, bmp.Height);
graphics.DrawString(captchatxt, font,
new SolidBrush(Color.Gold), 25, 10);
graphics.Flush();
font.Dispose();
graphics.Dispose();
}
return bmp;
}
}
}
|
In above
code I have create the bit map and of the image and saved the captcha value in
the session. This session value we will
use to validate the entered captcha code.
After this
we will create the view for the captcha class controller get method.
@{
ViewBag.Title
=
"CaptchaIndex";
}
|
After this
we will add a new controller file where we will display the captcha and perform
validation of captcha.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace
MVC_Demos.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
[HttpGet]
public ActionResult
Index()
{
return View();
}
[HttpPost]
public ActionResult
Index(
string
captchaValue)
{
if (captchaValue == Convert.ToString(Session["Captcha"]))
{
ViewBag.Message =
"You have
enter correct captcha."
;
}
else
{
ViewBag.Message =
"Pleae
enter valid captcha."
;
}
return View();
}
}
}
|
In above code I have added httpget and httpost
method. In this post method I have
captured and validated weather the user have added correct captcha code or not.
Now we will
create the view and add the below code.
@{
ViewBag.Title
=
"Index";
}
<h2>Validate Captcha</h2><br />
@using (Html.BeginForm("Index", "Home"))
{
@ViewBag.Message<br />
<img src="/Captcha/CaptchaIndex/" /><br /><br />
<input type="text" name="captchaValue" id="txtCaptcha"
/><br /><br />
<input type="submit" value="Submit" />
}
|
In above
code I have bind the captchaindex view to the image control. This will be
responsible for displaying the captcha code.