Asp.Net MVC 2 CAPTCHA(güvenlik kodu) Uygulaması
Asp.Net 4 mvc 2 de CAPTCHA uygulamasının nasıl yapıldığını hep beraber görelim.
ilk önce yeni proje başlatalım.uygulamayı Visual C# web olarak seçiyoruz.
projemizin adı CAPTCHA olarak ayarlıyoruz.Aşağıdaki Resim gibi...
index.aspx deki ContentPlaceHolderID="MainContent" ın içine alttaki kodları ekliyoruz.
<h2><%= Html.Encode(ViewData["Message"]) %></h2>
<% using (Html.BeginForm("index", "home")) { %>
<p><img src="/home/getcaptcha" /></p>
<p>Lütfen Kodu Yazınız::</p>
<p><%=Html.TextBox("captcha")%></p>
<p><input type="submit" value="Gönder" /></p>
<% } %>
Daha sonra Controllers klasöründeki HomeController.cs e aşağıdaki kodları ekliyoruz.using CAPTCHA.Models;
[HttpPost]
public ActionResult Index(string captcha)
{
if (captcha == HttpContext.Session["captcha"].ToString())
ViewData["Message"] = "Kodlar doğru!";
else
ViewData["Message"] = "Kodlar yanlış.tekrar deneyin.!";
return View();
}
public CaptchaResult GetCaptcha()
{
string captchaText = Captcha.GenerateRandomCode();
HttpContext.Session.Add("captcha", captchaText);
return new CaptchaResult(captchaText);
}
Şimdi de Models klasörüne sağ tıklayıp add class diyoruz.Clasımızın adı ise Captcha.cs olacak.
Captcha.cs ın kodları ise şöyle;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace CAPTCHA.Models
{
public class Captcha
{
private string text;
private int width;
private int height;
private string familyName;
private Bitmap image;
private static Random random = new Random();
public string FamilyName
{
get { return familyName; }
set { familyName = value; }
}
public string Text
{
get { return this.text; }
set { text = value; }
}
public Bitmap Image
{
get
{
if (!string.IsNullOrEmpty(text) && height > 0 && width > 0)
GenerateImage();
return this.image;
}
}
public int Width
{
get { return this.width; }
set { width = value; }
}
public int Height
{
get { return this.height; }
set { height = value; }
}
public Captcha()
{
}
~Captcha()
{
Dispose(false);
}
public void Dispose()
{
GC.SuppressFinalize(this);
this.Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
this.image.Dispose();
}
private void SetDimensions(int width, int height)
{
if (width <= 0)
throw new ArgumentOutOfRangeException("width", width, "Argument out of range, must be greater than zero.");
if (height <= 0)
throw new ArgumentOutOfRangeException("height", height, "Argument out of range, must be greater than zero.");
this.width = width;
this.height = height;
}
private void SetFamilyName(string familyName)
{
try
{
Font font = new Font(this.familyName, 16F);
this.familyName = familyName;
font.Dispose();
}
catch
{
this.familyName = FontFamily.GenericSerif.Name;
}
}
public void GenerateImage()
{
Bitmap bitmap = new Bitmap(this.width, this.height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bitmap);
g.SmoothingMode = SmoothingMode.AntiAlias;
Rectangle rect = new Rectangle(0, 0, this.width, this.height);
HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, Color.FromArgb(114, 172, 236), Color.FromArgb(161, 214, 255));
g.FillRectangle(hatchBrush, rect);
SizeF size;
float fontSize = this.height + 4;
Font font;
do
{
fontSize--;
font = new Font(this.familyName, fontSize, FontStyle.Bold);
size = g.MeasureString(this.text, font);
} while (size.Width > this.width);
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
GraphicsPath path = new GraphicsPath();
path.AddString(this.text, font.FontFamily, (int)font.Style, font.Size, rect, format);
float v = 4F;
PointF[] points =
{
new PointF(random.Next(this.width) / v, random.Next(this.height) / v),
new PointF(this.width - random.Next(this.width) / v, random.Next(this.height) / v),
new PointF(random.Next(this.width) / v, this.height - random.Next(this.height) / v),
new PointF(this.width - random.Next(this.width) / v, this.height - random.Next(this.height) / v)
};
Matrix matrix = new Matrix();
matrix.Translate(0F, 0F);
path.Warp(points, rect, matrix, WarpMode.Perspective, 0F);
hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, ColorTranslator.FromHtml("#000000"), ColorTranslator.FromHtml("#000000"));
g.FillPath(hatchBrush, path);
int m = Math.Max(this.width, this.height);
for (int i = 0; i < (int)(this.width * this.height / 30F); i++)
{
int x = random.Next(this.width);
int y = random.Next(this.height);
int w = random.Next(m / 50);
int h = random.Next(m / 50);
g.FillEllipse(hatchBrush, x, y, w, h);
}
font.Dispose();
hatchBrush.Dispose();
g.Dispose();
this.image = bitmap;
}
public static string GenerateRandomCode()
{
string s = "";
for (int i = 0; i < 6; i++)
s = String.Concat(s, random.Next(10).ToString());
return s;
}
}
}
Son olarak Models klasörüne CaptchaResult.cs adında bir class ekliyoruz.using System.Drawing.Imaging;
using System.Web;
using System.Web.Mvc;
namespace CAPTCHA.Models
{
public class CaptchaResult : ActionResult
{
public string _captchaText;
public CaptchaResult(string captchaText)
{
_captchaText = captchaText;
}
public override void ExecuteResult(ControllerContext context)
{
Captcha c = new Captcha();
c.Text = _captchaText;
c.Width = 200;
c.Height = 50;
c.FamilyName = "Century Schoobook";
HttpContextBase cb = context.HttpContext;
cb.Response.Clear();
cb.Response.ContentType = "image/jpeg";
c.Image.Save(cb.Response.OutputStream, ImageFormat.Jpeg);
c.Dispose();
}
}
}
İşte bu kadar...
Yorumlar
Yorum Gönder