生成验证码并验证

1.新建HttpHandle处理程序,减小访问服务器压力

<%@ WebHandler Language="C#" Class="ValidHandle" %>

using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

public class ValidHandle : IHttpHandler
{

    private string CheckCode()
    {
        int number;
        string strCode = string.Empty;
        //随机种子
        Random random = new Random();
        for (int i = 0; i < 4; i++)
        {//四位校验码
            number = random.Next();
            //字符从0-9,A—Z中随机产生,对应的AscII码为48-57,65-90
            number = number % 36;
            if (number < 10)
            {
                number += 48;
            }
            else
            {
                number += 55;
            }
            strCode += ((char)number).ToString();
        }
        //在Cookie中保存校验码
        
       
        return strCode;
    }

    public void ProcessRequest(HttpContext context)
    {

        Bitmap image = new Bitmap(60, 30);
        Graphics g = Graphics.FromImage(image);
        g.Clear(Color.White);
        g.FillRectangle(Brushes.WhiteSmoke, 0, 0, 60, 30);
        g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width-2, image.Height-2);

        Random random = new Random();
        int i;
        for (i = 0; i < 25; i++)
        {
            int x1 = random.Next(image.Width);
            int x2 = random.Next(image.Width);
            int y1 = random.Next(image.Height);
            int y2 = random.Next(image.Height);
            g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
        }
        Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold));
        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);
        string code = CheckCode();
        g.DrawString(code, font, brush, 4, 5);        
        
        
        using (MemoryStream ms = new MemoryStream())
        {
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);//保存图片格式为png
            context.Response.ClearContent();
            context.Response.ContentType = "image/Png";
            //输出二进制流
            context.Response.BinaryWrite(ms.ToArray());
            context.Response.Cookies.Add(new HttpCookie("CheckCode", code));         
        }
        image.Dispose();
        g.Dispose();
            }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}


2.显示页面中使用img标签,链接到HttpHandle处理程序页面

<form id="form1" runat="server">
     验证码:<asp:TextBox ID="validCode" runat="server" ></asp:TextBox>     
     <img  alt="" src="ValidHandle.ashx" />
     <asp:Button ID="buttonOK" runat="server" Text="submit"
         onclick="buttonOK_Click" />
    </form>


3. 验证,使用cookies获取生成的验证码

protected void buttonOK_Click(object sender, EventArgs e)
    {
        string vCode = validCode.Text.Trim();
        string checkCode = Request.Cookies["CheckCode"].Value.ToString();
        if (vCode != checkCode)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "net111", "<script >alert('验证码不正确!')</script>");
            this.validCode.Text = "";
            return;
        }
        else
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "net111", "<script >alert('验证码正确!')</script>");

        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值