ASP.NET自己寫的驗證碼控制項(原始碼)

來源:互聯網
上載者:User

1,建立一個ASP.NET的伺服器控制項

2,在伺服器控制項裡面建立一個類,賦值如下

3,產生之後直接在網頁拖動使用

4,調用 bool b = this.ValidateNumber1.CheckSn(this.TextBox1.Text);就OK

5,這個只是比較基礎的自己弄的玩玩,不過也過得去

 

 

--------------------------------源碼----------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

namespace ValidateNumber
{
    public class ValidateNumber:Control
    {
        public void CreateCode() {
            //1,定義驗證碼的顏色,早點,和顯示的字元
            //顏色列表,用於驗證碼,噪點,噪線
            Color[] color = { Color.Black, Color.Red, Color.Black, Color.Green, Color.Orange, Color.Brown, Color.DarkBlue };
            //字型列表,用於驗證碼
            string[] font = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiu", "Impact" };
            //驗證碼的字元集,去掉了一些容易混淆的字元
            char[] character = { '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
            Random rnd = new Random();
            //2,隨即產生和儲存驗證碼
            string chkCode = "";
            //產生驗證字串
            for (int i = 0; i < 4; i++)
            {
                chkCode += character[rnd.Next(character.Length)];
            }
            //儲存驗證碼採用cookie模式
            HttpResponse resp = this.Page.Response;
            resp.Cookies["validateCookie"].Value = chkCode;
            resp.Cookies["validateCookie"].Expires = DateTime.Now.AddMinutes(2);
            //3,繪製噪點,噪線
            Bitmap bmp = new Bitmap(100, 30);//長100排序,寬30px的圖片
            Graphics g = Graphics.FromImage(bmp);
            g.Clear(Color.White);
            //畫五條噪線
            for (int i = 0; i < 5; i++)
            {
                //隨機產生噪線的起點和終點
                int x1 = rnd.Next(100);
                int y1 = rnd.Next(30);
                int x2 = rnd.Next(100);
                int y2 = rnd.Next(30);
                //隨機產生噪線的顏色
                Color clr = color[rnd.Next(color.Length)];
                //畫出噪線
                g.DrawLine(new Pen(clr), x1, y1, x2, y2);
            }
            //4,畫噪點
            for (int i = 0; i < 100; i++)
            {
                int x = rnd.Next(bmp.Width);
                int y = rnd.Next(bmp.Height);
                Color clr = color[rnd.Next(color.Length)];
                bmp.SetPixel(x, y, clr);
            }
            //5,畫驗證字串
            for (int i = 0; i < chkCode.Length; i++)
            {
                string fnt = font[rnd.Next(font.Length)];
                Font ft = new Font(fnt, 16);
                Color clr = color[rnd.Next(color.Length)];
                g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 20, (float)6);
            }
            //6,將驗證碼圖片寫入記憶體流,並將其以image/Png格式輸出
            MemoryStream ms = new MemoryStream();//記憶體流
            try
            {
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                resp.ClearContent();
                resp.ContentType = "image/Png";
                resp.BinaryWrite(ms.ToArray());
                resp.Flush();
                resp.End();
            }
            finally
            {
                //釋放資源
                bmp.Dispose();
                g.Dispose();
            }
        }

        //7,實現驗證方法
        public bool CheckSn(string sn) {
            return (sn.ToUpper() == this.Page.Request.Cookies["validateCookie"].Value.ToString().ToUpper());
        }

        //8,建立驗證碼
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            //開發環境設計檢視
            if (this.DesignMode) {
                return;
            }
            //如果不是驗證碼請求
            string str=this.Page.Request.QueryString["_ImageTag"];
            if (str == "1") {
                this.CreateCode();//產生驗證碼
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (!this.DesignMode) { //設計檢視
                writer.Write("<img border=\"1\" src=\"{0}\" style='width:100px height:30px'>",this.Page.Request.Path+"?_ImageTag=1");
            }else{
                writer.Write("驗證碼控制項");
            }
        }
    }
}

聯繫我們

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