利用HttpHandler实现验证码 及 当你希望从ashx或HttpHandler里访问你的Session时,你必须实现IReadOnlySessionState接口.
Posted on 2008-08-19 10:49 wisdom1217 阅读(819) 评论(0) 编辑 收藏 举报输出图片并保存验证码在Session里面。
注意:
必须继承System.Web.SessionState.IRequiresSessionState接口,才能实现Session读写!
System.Web.SessionState的一些接口
IReadOnlySessionState 指定目标 HTTP 处理程序只需要具有对会话状态值的读访问权限。这是一个标记接口,没有任何方法。
IRequiresSessionState 指定目标 HTTP 处理程序需要对会话状态值具有读写访问权。这是一个标记接口,没有任何方法。
ValidateCode.ashx---------------------------------------------------
<%@ WebHandler Language="C#" Class="ValidateCode" %>
using System;
using System.Web;
using System.Web.SessionState;
/// <summary>
/// 验证码程序
/// </summary>
public class ValidateCode : IHttpHandler, IRequiresSessionState
{
/// <summary>
/// 入口
/// </summary>
/// <param name="context"></param>
public void ProcessRequest(HttpContext context)
{
string checkCode = CreateRandomCode(4);
context.Session["Code"] = checkCode;
CreateImage(checkCode, context);
}
public bool IsReusable
{
get
{
return false;
}
}
/// <summary>
/// 产生验证码
/// </summary>
/// <param name="codeCount"></param>
/// <returns></returns>
private string CreateRandomCode(int codeCount)
{
//验证码中的出现的字符,避免了一些容易混淆的字符。
string allChar = "3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,M,N,P,Q,R,S,T,U,W,X,Y";
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -1;
Random rand = new Random();
for (int i = 0; i < codeCount; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(allCharArray.Length);
if (temp == t)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
/// <summary>
/// 创建图片
/// </summary>
/// <param name="checkCode"></param>
/// <param name="context"></param>
private void CreateImage(string checkCode, HttpContext context)
{
int iwidth = (int)(checkCode.Length * 12);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
g.Clear(System.Drawing.Color.White);
//定义颜色
System.Drawing.Color[] c = { System.Drawing.Color.DimGray, System.Drawing.Color.DimGray, System.Drawing.Color.DimGray };
//定义字体
string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
Random rand = new Random();
//随机输出噪点
for (int i = 0; i < 50; i++)
{
int x = rand.Next(image.Width);
int y = rand.Next(image.Height);
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.LightGray, 0), x, y, 1, 1);
}
//输出不同字体和颜色的验证码字符
for (int i = 0; i < checkCode.Length; i++)
{
int cindex = rand.Next(3);
int findex = rand.Next(1);
System.Drawing.Font f = new System.Drawing.Font(font[findex], 10, System.Drawing.FontStyle.Bold);
System.Drawing.Brush b = new System.Drawing.SolidBrush(c[cindex]);
int ii = 4;
if ((i + 1) % 2 == 0)
{
ii = 2;
}
g.DrawString(checkCode.Substring(i, 1), f, b, 3 + (i * 10), ii);
}
//画一个边框
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.DarkGray, 0), 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();
}
}
页面
<img src="ashx文件地址">
判断验证码是否正确
Session["Code"].ToString()==输入的字符
==========================================
当你希望从ashx或HttpHandler里访问你的Session时,你必须实现IReadOnlySessionState接口.
当你希望从ashx或HttpHandler里访问你的Session时,你必须实现IReadOnlySessionState接口.
代码:
using System;
using System.Web;
using System.Web.SessionState;
public class DownloadHandler : IHttpHandler, IReadOnlySessionState
{
public bool IsReusable { get { return true; } }
public void ProcessRequest(HttpContext ctx)
{
ctx.Response.Write(ctx.Session["fred"]);
}
}