ASP.NET中如何使用驗證碼效驗

來源:互聯網
上載者:User

主要功能:

  * 產生驗證碼

  * 效驗驗證碼

  基本原理:

  根據一定的規則產生隨機的5為字元(由0—9的數字和A—Z的字母組成),並寫入Session。驗證的時候再從Session中取出進行比較。

  前提知識:

  關於ashx檔案

  本質:缺少html檔案的asp教程x檔案。

  使用情境:

  適合產生動態映像或文本。

  ashx輸出作為頁面元素img的背景(屬性src的值,eg:<img src="../Handler/WaterMark.ashx" id="vimg" alt="" onclick="change()" />)

  .ashx檔案有個缺點,他處理控制項的回傳事件非常麻煩,比如說如果用它來產生DataGrid的列表也不是不行,但是處理資料的回傳,需要一些.aspx頁的功能,只有自己手動處理這些功能。所以,一般使用.ashx,用來輸出一些不需要回傳處理的項目即可。

  程式設計

  1 <%@ WebHandler Language="C#" Class="WaterMark" %>

  2

  3 using System;

  4 using System.Web;

  5 using System.Drawing;

  6 using System.Drawing.Drawing2D;

  7 using System.Web.SessionState;

  8

  9 public class WaterMark : IHttpHandler,IRequiresSessionState{

  10     //使用Session時必須實現IRequiresSessionState介面,並引入命名空間System.Web.SessionState

  11     public void ProcessRequest (HttpContext context) {

  12         string checkCode = GenCode(5);

  13         context.Session["Code"] = checkCode;

  14         System.Drawing.Bitmap image = new System.Drawing.Bitmap(70, 22);

  15         Graphics g = Graphics.FromImage(image);

  16         try

  17         {

  18             Random random = new Random();

  19

  20             g.Clear(Color.White);

  21

  22             int i;

  23             for (i = 0; i < 25; i++)

  24             {

  25                 int x1 = random.Next(image.Width);

  26                 int x2 = random.Next(image.Width);

  27                 int y1 = random.Next(image.Height);

  28                 int y2 = random.Next(image.Height);

  29       g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);

 

  30             }

  31
  

  32             Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold));

  33             System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2F, true);

  34             g.DrawString(checkCode, font, brush, 2, 2);

  35

  36             g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

  37             System.IO.MemoryStream ms = new System.IO.MemoryStream();

  38             image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

  39             context.Response.ClearContent();

  40             context.Response.ContentType = "image/png";

  41             context.Response.BinaryWrite(ms.ToArray());

  42         }

  43         finally {

  44             g.Dispose();

  45             image.Dispose();

  46         }

  47     }

  48

  49     //產生隨機字串

  50     private string GenCode(int num) {

  51         string str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

  52         char[] chastr = str.ToCharArray();

  53         string code = "";

  54         Random rd = new Random();

  55         int i;

  56         for (i = 0; i < num;i++ )

  57         {

  58             code += str.Substring(rd.Next(0, str.Length), 1);

  59         }

  60         return code;

  61     }

  62     public bool IsReusable {

  63         get {

  64             return false;

  65         }

  66     }

  67

  68 }ASP.NET中如何使用驗證碼效驗

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.