تصویر امنیتی CAPTCHA در Asp.Net
شنبه 27 اردیبهشت 1393امروزه با زیاد شدن رباط های وبگرد و خرابکاری این رباط ها باید از خرابکاری هاشون جلوگیری کنیم <br/> یکی از این راه ها استفاده از تصویر امنیتی هست که قصد داریم در این مقاله بررسی کنیم

سلام دوستان
امروزه با زیاد شدن رباط های وبگرد و خرابکاری این رباط ها باید از خرابکاری هاشون جلوگیری کنیم
یکی از این راه ها استفاده از تصویر امنیتی هست که قصد داریم در این مقاله بررسی کنیم
ابتدا در صفحه خود کنترل های زیر را ایجاد میکنیم
<table> <tr> <td colspan="3"> <asp:Label ID="lblCaptchaResult" runat="server" /> <%------ Label to show result wether we have passed/failed captcha.--%> </td> </tr> <tr> <td> <asp:TextBox runat="server" ID="txtImg" /> </td> <td> <asp:Image ID="imgCaptcha" runat="server" /> <%------ Image to hold captcha value -----%> </td> <td> <asp:Button Text="چک کن" ID="btnSubmit" OnClick="btnSubmit_Click" runat="server" /> </td> </tr> </table>
یک کنترل تصویر برای نمایش تصویر امنیتی و یک Lable برای نمایش نتیجه
سپس یک Geniric handler با نام "CaptchaHandler.ashx" جهت ساخت تصویر امنیتی می سازیم
کدها به شرح زیر است :
public void ProcessRequest(HttpContext context) { string s = context.Request.QueryString.Get("txt"); context.Response.ContentType = "image/gif"; CreateImage(s).Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif); } private static Bitmap CreateImage(string sImageText) { Bitmap bmpImage = new Bitmap(1, 1); int iWidth = 0; int iHeight = 0; Font MyFont = new Font("Arial", 18, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel); Graphics MyGraphics = Graphics.FromImage(bmpImage); iWidth = Convert.ToInt32(MyGraphics.MeasureString(sImageText, MyFont).Width) + 20; iHeight = Convert.ToInt32(MyGraphics.MeasureString(sImageText, MyFont).Height) + 4; bmpImage = new Bitmap(bmpImage, new Size(iWidth, iHeight)); MyGraphics = Graphics.FromImage(bmpImage); MyGraphics.Clear(Color.Beige); MyGraphics.TextRenderingHint = TextRenderingHint.AntiAlias; MyGraphics.DrawString(sImageText, MyFont, new SolidBrush(Color.Brown), 10, 4); MyGraphics.Flush(); return (bmpImage); } public bool IsReusable { get { return true; } }
سپس در قسمت کد صفحه مورد نظر متدی با نام CreateRandomString() میسازیم که یک عدد ورودی دریافت میکند ، این عدد طول رشته برای ساخت تصویر است
//---- Method to create random string to be used as captcha. public void CreateRandomString(int length) { string guidResult = System.Guid.NewGuid().ToString(); guidResult = guidResult.Replace("-", string.Empty); guidResult = guidResult.Substring(0, length); imgCaptcha.ImageUrl = "~/CaptchaHandler.ashx?txt=" + guidResult; Session["RandomImgText"] = guidResult; }
متد دیگری با نام ValidateForm برای چک کردن صحیح بودن عبارت وارد شده ایجاد میکنیم
//--- Method to check whether entered captcha is correct or not. protected bool ValidateForm() { bool IsValid = true; if (txtImg.Text != Session["RandomImgText"].ToString()) { IsValid = false; } return IsValid; }
سپس در رویداد Page_Load تصویر را ایجاد میکنیم
protected void Page_Load(object sender, System.EventArgs e) { if (!IsPostBack) { //--- You can increase or decrease numbers //--- to increase or decrease captcha strings. CreateRandomString(6); } }
سپس در کلید چک کن Button_Click کدهای زیر را مینویسیم
bool status = ValidateForm(); if (status) { lblCaptchaResult.Text = "معتبر است"; lblCaptchaResult.ForeColor = Color.Green; } else { lblCaptchaResult.Text = "عبارت وارد شده صحیح نمی باشد"; lblCaptchaResult.ForeColor = Color.Red; } //--- Recreate captcha. CreateRandomString(6);
خب امیدوارم به کارتون بیاد
موفق و پیروز باشید
نمونه هم ضمیمه کردم
- ASP.net
- 7k بازدید
- 10 تشکر