Asp.Net驗證碼

來源:互聯網
上載者:User

標籤:

驗證碼html調用

驗證碼:<input name="txtCode0" type="text" id="txtCode0" class="login-text" size="10" maxlength="4"><img src="CodeHandler.ashx" id="imgCode" alt="驗證碼" title="點擊重新整理驗證碼" align="middle"                                    style="height: 24px; width: 70px;" onclick="refreshRandCode()">

驗證碼重新整理

//重新整理驗證碼        function refreshRandCode() {            $(‘#imgCode‘).hide().attr(‘src‘,            ‘CodeHandler.ashx?‘ + Math.floor(Math.random() * 100)).fadeIn();        }

驗證碼後台判斷

Session["checkCode"]  

 

驗證碼CodeHandler.ashx

難以分清的的字母i、o已刪除

<%@ WebHandler Language="C#" Class="CodeHandler" %>using System;using System.Web;using System.Drawing;public class CodeHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState{    public string charSet = "0,1,2,3,4,5,6,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z";    public void ProcessRequest (HttpContext context) {        string validateCode = CreateRandomCode(4);        context.Session["checkCode"] = validateCode;        CreateImage(validateCode, context);       }     public bool IsReusable {        get {            return false;        }    }    /// <summary>        /// 產生驗證碼              /// <param name="n">位元</param>       /// <returns>驗證碼字串</returns>       private string CreateRandomCode(int n)    {        string[] CharArray = charSet.Split(‘,‘);        string randomCode = "";        int temp = -1;        Random rand = new Random();        for (int i = 0; i < n; i++)        {            if (temp != -1)            {                rand = new Random(i * temp * ((int)DateTime.Now.Ticks));            } int t = rand.Next(CharArray.Length - 1);            if (temp == t)            {                return CreateRandomCode(n);            } temp = t;            randomCode += CharArray[t];        }        return randomCode;    }    private void CreateImage(string checkCode, HttpContext context)    {        int iwidth = (int)(checkCode.Length * 14+10);        System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 23);        Graphics g = Graphics.FromImage(image);        Font f = new System.Drawing.Font("Arial", 14, (System.Drawing.FontStyle.Italic | System.Drawing.FontStyle.Bold));        // 前景色彩                Brush b = new System.Drawing.SolidBrush(Color.Black);        // 背景色               g.Clear(Color.White);        // 填充文字                g.DrawString(checkCode, f, b, 0, 1);        // 隨機線條                Pen linePen = new Pen(Color.Gray, 0);         Random rand = new Random();        for (int i = 0; i < 10; i++)        {            int x1 = rand.Next(image.Width);            int y1 = rand.Next(image.Height);            int x2 = rand.Next(image.Width);            int y2 = rand.Next(image.Height);            g.DrawLine(linePen, x1, y1, x2, y2);        }        // 隨機點                for (int i = 0; i < 50; i++)        {            int x = rand.Next(image.Width);            int y = rand.Next(image.Height);            image.SetPixel(x, y, Color.Gray);        }        // 邊框               g.DrawRectangle(new Pen(Color.Gray), 0, 0, image.Width - 1, image.Height - 1);        // 輸出圖片          System.IO.MemoryStream ms = new System.IO.MemoryStream();        image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);        context.Response.ClearContent();        context.Response.ContentType = "image/Jpeg";        context.Response.BinaryWrite(ms.ToArray());        g.Dispose();        image.Dispose();    }    }

 

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.