asp.net 2.0 驗證模組的實現

來源:互聯網
上載者:User
1 搞一個模組,編譯成DLL
  

namespace ASPNETAJAXWeb.ValidateCode.Page
{
 public class ValidateCode:System.Web.UI.Page
 {
  private const double IMAGELENGTHBASE = 12.5;
  private const int IMAGEHEIGTH = 22;
  private const int IMAGELINENUMBER = 25;
  private const int IMAGEPOINTNUMBER = 100;
  public static string VALIDATECODEKEY = "VALIDATECODEKEY";

  private int length = 4;
  private string code = string.Empty;

  /// <summary>
  /// 擷取或設定驗證碼長度,預設值為4。
  /// </summary>
  public int Length
  {
   get
   {
    return length;
   }
   set
   {
    length = value;
   }
  }

  /// <summary>
  /// 擷取驗證碼
  /// </summary>
  public string Code
  {
   get
   {
    return Code;
   }
  }

  public ValidateCode()
  {

  }

  protected override void OnLoad(EventArgs e)
  {
   CreateValidateImage(length);
  }

  /// <summary>
  /// 建立隨機驗證碼
  /// </summary>
  /// <param name="length">驗證碼長度</param>
  /// <returns></returns>
  public string CreateCode(int length)
  {
   if(length <= 0) return string.Empty;
   ///建立一組隨機數,並構成驗證碼
   Random random = new Random();
   StringBuilder sbCode = new StringBuilder();
   for(int i = 0; i < length; i++)
   {
    sbCode.Append(random.Next(0,10));
   }
   ///儲存驗證碼到Session對象中
   code = sbCode.ToString();
   Session[VALIDATECODEKEY] = code;
   return code;
  }

  /// <summary>
  /// 建立驗證碼的圖片和驗證碼
  /// </summary>
  /// <param name="length">驗證碼的長度</param>
  public void CreateValidateImage(int length)
  {   ///建立驗證碼
   code = CreateCode(length);
   ///建立驗證碼的圖片
   CreateValidateImage(code);
  }

  /// <summary>
  /// 建立驗證碼的圖片和驗證碼
  /// </summary>
  /// <param name="code">驗證碼</param>
  public void CreateValidateImage(string code)
  {
   if(string.IsNullOrEmpty(code) == true) return;
   ///儲存驗證碼到Session對象中
   Session[VALIDATECODEKEY] = code;
   ///建立一個映像
   Bitmap image = new Bitmap((int)Math.Ceiling((code.Length * IMAGELENGTHBASE)),IMAGEHEIGTH);
   Graphics g = Graphics.FromImage(image);

   ///隨機數產生器
   Random random = new Random();

   try
   {
    ///清空映像,並指定填充顏色
    g.Clear(Color.White);

    ///繪製圖片的幹擾線
    int x1,x2,y1,y2;
    for(int i = 0; i < IMAGELINENUMBER; i++)
    {
     x1 = random.Next(image.Width);
     y1 = random.Next(image.Height);
     x2 = random.Next(image.Width);
     y2 = random.Next(image.Height);
     ///繪製幹擾線
     g.DrawLine(new Pen(Color.Silver),x1,y1,x2,y2);
    }

    ///繪製驗證碼
    Font font = new Font("Tahoma",12,FontStyle.Bold | FontStyle.Italic);
    LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0,0,image.Width,image.Height),
     Color.Blue,Color.DarkRed,1.2f,true);
    g.DrawString(code,font,brush,2.0f,2.0f);

    ///畫圖片的前景噪音點
    int x,y;
    for(int i = 0; i < IMAGEPOINTNUMBER; i++)
    {
     x = random.Next(image.Width);
     y = random.Next(image.Height);
     ///繪製點
     image.SetPixel(x,y,Color.FromArgb(random.Next()));
    }

    ///畫圖片的邊框線
    g.DrawRectangle(new Pen(Color.Silver),0,0,image.Width - 1,image.Height - 1);
    ///儲存圖片的內容到流中
    MemoryStream ms = new MemoryStream();
    image.Save(ms,ImageFormat.Gif);
    ///輸出圖片
    Response.ClearContent();
    Response.ContentType = "image/Gif";
    Response.BinaryWrite(ms.ToArray());
   }
   finally
   {   ///釋放佔有的資源
    g.Dispose();
    image.Dispose();
   }
  }
 }
}

 2  搞一個validatecode.asp頁
  <%@ Page Language="C#" AutoEventWireup="false"  Inherits="ASPNETAJAXWeb.ValidateCode.Page.ValidateCode" %>
3 在要用驗證碼的地方,引用編譯好的DLL
4 要用的地方
   <tr bgcolor="white">      
     <td>驗 證 碼:</td>
     <td>
      <asp:TextBox ID="tbCode" runat="server" SkinID="tbSkin" Width="80px"></asp:TextBox>
      <asp:Image ID="imgCode" runat="server" ImageUrl = "~/ValidateCode.aspx" />           
     </td>
    </tr> 
5 邏輯代碼
   //判斷是否建立了驗證嗎
  if(Session[ValidateCode.VALIDATECODEKEY] == null) return;
  ///驗證驗證碼是否相等
  if(tbCode.Text != Session[ValidateCode.VALIDATECODEKEY].ToString())
  {
   lbMessage.Text = "驗證碼輸入錯誤,請重新輸入。";
   return;
  }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.