一般处理程序制作的验证码

一、新建一个ValidateCode.ashx文件

<%@ WebHandler Language="C#" Class="ValidateCode"Debug="true" %>

//Debug="true"是加上去的

using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web.SessionState;//要使用Session必须使用命名空间里的接口IRequiresSessionState

public class ValidateCode : IHttpHandler,IRequiresSessionState{
      public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "image/jpeg";
        //context.Response.Write("Hello World"+MakeRandomNum());测试生成随机验证码
   //创建验证图片
    string vCode=MakeRandomNum();
    context.Session["vcode"] = vCode;//给Session里写验证码值
    using (Bitmap img = new Bitmap(80,30))
      {
       using (Graphics g = Graphics.FromImage(img))
        {
          g.DrawString(vCode,new Font("微软雅黑",16),Brushes.White,10,2);
          img.Save(context.Response.OutputStream,ImageFormat.Jpeg);
         }
       }
      }
    //生成随机验证码
    public string MakeRandomNum()
    {
        Random ran = new Random();
        string resNum = string.Empty;
        for (int i = 0; i < 4; i++)
        {
            resNum += ran.Next(9).ToString();
        }
        return resNum;
    }
      public bool IsReusable {
        get {
            return false;
             }
       }
}

二、登陆页面C05LoginBySession.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="C05LoginBySession.aspx.cs" Inherits="C05LoginBySession" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" action="C05LoginBySession.aspx"method="post">
    <input id="txtName"name="txtName"type="text"/><br/>
    <input id="txtPwd"name="txtPwd"type="text"/><br/>
    <input id="txtCode"name="txtCode"type="text"/><img src="ValidateCode.ashx"/><br/>
    <input id="Submit1"type="submit"value="登陆"/>
    <a href="Reg.aspx">注册</a>
    <input type="hidden"name="IsPostBack"value="1"/>
    </form>
</body>
</html>
三、登陆页面C05LoginBySession.aspx.cs  到Session读值判断验证码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class C05LoginBySession : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.Form["IsPostBack"]))
        {
            string strCode=Request.Form["txtCode"];
            if (!string.IsNullOrEmpty(strCode) && Session["vcode"] != null && strCode == Session["vcode"].ToString())
            {
                string strName = Request.Form["txtName"];
                string strPwd = Request.Form["txtPwd"];
                Session["uname"] = strName;
                Session["udate"] = DateTime.Now.ToString();
                Response.Redirect("IndexBySession.aspx");
            }
            else
            {
                Response.Write("验证码错了");
            }
        }
    }
}

四、图片

五、源码下载

posted on 2011-08-14 23:15  smxlcs  阅读(339)  评论(0)    收藏  举报