利用HttpHandler可以实现很多功能,比如图片防盗、模块权限设置等,今天我们来介绍HttpHandler实现验证码功能
包含以下几个功能点:
1、初始化页面时生成验证码
2、客户端点击更换验证码
3、后台验证验证码是否输入正确
方法一、新建一个一般处理文件GetCheckCode.ashx,然后在用的地方引用URL即可
1、GetCheckCode.ashx
说明:实现IRequiresSessionState接口是因为要对Session对象进行写操作
<%@ WebHandler Language="C#" Class="GetCheckCode" %>
using System;
using System.Web;
using System.Drawing;
using System.Text;
using System.Drawing.Imaging;
using System.Web.SessionState;
public class GetCheckCode : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest (HttpContext context) {
//context.Response.ContentType = "image/gif";
//建立Bitmap对象,绘图
System.Drawing.Bitmap basemap = new Bitmap(200, 60);
System.Drawing.Graphics graph = Graphics.FromImage(basemap);
graph.FillRectangle(new SolidBrush(Color.White), 0, 0, 200, 60);
Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel);
Random r = new Random();
string letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ";
string letter;
StringBuilder s = new StringBuilder();
//添加随机的五个字母
for (int x = 0; x < 5; x++)
{
letter = letters.Substring(r.Next(0, letters.Length - 1), 1);
s.Append(letter);
graph.DrawString(letter, font, new SolidBrush(Color.Black), x * 38, r.Next(0, 15));
}
//混淆背景
Pen linePen = new Pen(new SolidBrush(Color.Black), 2);
for (int x = 0; x < 6; x++)
graph.DrawLine(linePen, new Point(r.Next(0, 199), r.Next(0, 59)), new Point(r.Next(0, 199), r.Next(0, 59)));
//方法一、将图片直接保存到输出流中
<span style="color:#FF0000;"><strong>basemap.Save(context.Response.OutputStream, ImageFormat.Gif);</strong></span>
方法二、将文件保存到服务器,再把文件以文件流的形式写入懂爱缓冲区
//string path = "Images/img" + DateTime.Now.ToString("yyyyMMdd HHmmss") + ".gif";
//basemap.Save(context.Server.MapPath(path), ImageFormat.Gif);
//context.Response.WriteFile(path);
<span style="color:#FF0000;"><strong>context.Session["CheckCode"] = s.ToString(); //如果没有实现IRequiresSessionState,则这里会出错,也无法生成图片</strong></span>
}
public bool IsReusable {
get {
return false;
}
}
}
2、需要验证码的页面
说明:更换图片时URL必须增加每次变动的参数,否则浏览器会误以为是相同的URL,图片将不不更新
CheckCode.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CheckCode.aspx.cs" Inherits="CheckCode" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script type="text/javascript">
function changeCode() {
<span style="color:#FF0000;"><strong>document.getElementById("imgCode").src = "GetCheckCode.ashx?t=" + (new Date()).valueOf();</strong></span>
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
请输入验证码:<span style="color:#FF0000;"><strong><asp:TextBox ID="txtCode" runat="server"></asp:TextBox><img id="imgCode" src="GetCheckCode.ashx" alt="验证码" /></strong></span>
<input type="button" id="btnChangeCode" value="换一张" οnclick="changeCode();" />
<asp:Button ID="btnCheckCode" runat="server" Text="核实验证码" OnClick="btnCheckCode_Click" />
</div>
</form>
</body>
</html>
3、页面后台
protected void btnCheckCode_Click(object sender, EventArgs e)
{
if (txtCode.Text.Trim() != Session["CheckCode"].ToString())
ClientScript.RegisterClientScriptBlock(GetType(), Guid.NewGuid().ToString(), "alert('验证码输入错误!')", true);
}
4、运行效果
方法二、新建一个实现IHttpHandler和IRequiresSessionState类,然后在Web.config中映射到一个文件,在页面中引用这个文件即可
1、GetCheckCodeHandler.cs
public class GetCheckCodeHandler : IHttpHandler, IRequiresSessionState
{
public GetCheckCodeHandler()
{
}
public void ProcessRequest(HttpContext context)
{
//context.Response.ContentType = "image/gif";
//建立Bitmap对象,绘图
System.Drawing.Bitmap basemap = new Bitmap(200, 60);
System.Drawing.Graphics graph = Graphics.FromImage(basemap);
graph.FillRectangle(new SolidBrush(Color.White), 0, 0, 200, 60);
Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel);
Random r = new Random();
string letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ";
string letter;
StringBuilder s = new StringBuilder();
//添加随机的五个字母
for (int x = 0; x < 5; x++)
{
letter = letters.Substring(r.Next(0, letters.Length - 1), 1);
s.Append(letter);
graph.DrawString(letter, font, new SolidBrush(Color.Black), x * 38, r.Next(0, 15));
}
//混淆背景
Pen linePen = new Pen(new SolidBrush(Color.Black), 2);
for (int x = 0; x < 6; x++)
graph.DrawLine(linePen, new Point(r.Next(0, 199), r.Next(0, 59)), new Point(r.Next(0, 199), r.Next(0, 59)));
//方法一、将图片直接保存到输出流中
basemap.Save(context.Response.OutputStream, ImageFormat.Gif);
方法二、将文件保存到服务器,再把文件以文件流的形式写入懂爱缓冲区
//string path = "Images/img" + DateTime.Now.ToString("yyyyMMdd HHmmss") + ".gif";
//basemap.Save(context.Server.MapPath(path), ImageFormat.Gif);
//context.Response.WriteFile(path);
context.Session["CheckCode"] = s.ToString(); //如果没有实现IRequiresSessionState,则这里会出错,也无法生成图片
}
public bool IsReusable
{
get
{
return false;
}
}
}
只需在原来页面的基础上把GetCheckCode.ashx改为GetCheckCode即可
3、Web.config
其中path可为任意格式,如GetCheckCode.aspx,无论该文件是否存在,请求都会被Asp.Net 处理程序捕获
<system.webServer>
<handlers>
<span style="color:#FF0000;"><strong><add name="GetCheckCode" type="DiyControlWeb.GetCheckCodeHandler" verb="GET" path="GetCheckCode"/></strong></span>
</handlers>
</system.webServer>
总结:
比较两种方法,第一种方法比较方便,新建一个ashx文件即可,第二种方法要自定义一个HttpHandler类,且需要在Web.config中配置。 从性能上来将,个人建议用第二种方法,因为一个ashx文件比一个类占有更多的空间。