日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

asp.net 2.0 驗證模塊的實現(xiàn)

 悟靜 2012-04-30
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>
  /// 獲取或設(shè)置驗證碼長度,默認值為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>
  /// 創(chuàng)建隨機驗證碼
  /// </summary>
  /// <param name="length">驗證碼長度</param>
  /// <returns></returns>
  public string CreateCode(int length)
  {
   if(length <= 0) return string.Empty;
   ///創(chuàng)建一組隨機數(shù),并構(gòu)成驗證碼
   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>
  /// 創(chuàng)建驗證碼的圖片和驗證碼
  /// </summary>
  /// <param name="length">驗證碼的長度</param>
  public void CreateValidateImage(int length)
  {   ///創(chuàng)建驗證碼
   code = CreateCode(length);
   ///創(chuàng)建驗證碼的圖片
   CreateValidateImage(code);
  }

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

   ///隨機數(shù)生成器
   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);
    ///保存圖片的內(nèi)容到流中
    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 邏輯代碼
   //判斷是否創(chuàng)建了驗證嗎
  if(Session[ValidateCode.VALIDATECODEKEY] == null) return;
  ///驗證驗證碼是否相等
  if(tbCode.Text != Session[ValidateCode.VALIDATECODEKEY].ToString())
  {
   lbMessage.Text = "驗證碼輸入錯誤,請重新輸入。";
   return;
  }

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約