使用ASP.NET 2.0 HttpHandler实现生成图片验证码的问题
2008-05-16 18:36
726 查看
最近做登录、注册界面,用到验证码,找了些资料。
在http://blog.csdn.net/chengking/archive/2007/01/09/1475140.aspx这篇文章中,给了使用ASP.NET 2.0 HttpHandler实现生成图片验证码的例子,还是不明白如何判断验证码是否正确,
ValidateImageHandler.ashx
<%@ WebHandler Language="C#" Class="ValidateImageHandler" %>
using System;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
/**//// <summary>
/// ValidateImageHandler 生成网站验证码功能
/// </summary>
public class ValidateImageHandler : IHttpHandler, IRequiresSessionState
...{
int intLength = 5; //长度
string strIdentify = "Identify"; //随机字串存储键值,以便存储到Session中
public ValidateImageHandler()
...{
}
/**//// <summary>
/// 生成验证图片核心代码
/// </summary>
/// <param name="hc"></param>
public void ProcessRequest(HttpContext hc)
...{
//设置输出流图片格式
hc.Response.ContentType = "image/gif";
Bitmap b = new Bitmap(200, 60);
Graphics g = Graphics.FromImage(b);
g.FillRectangle(new SolidBrush(Color.YellowGreen), 0, 0, 200, 60);
Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel);
Random r = new Random();
//合法随机显示字符列表
string strLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
StringBuilder s = new StringBuilder();
//将随机生成的字符串绘制到图片上
for (int i = 0; i < intLength; i++)
...{
s.Append(strLetters.Substring(r.Next(0, strLetters.Length - 1), 1));
g.DrawString(s[s.Length - 1].ToString(), font, new SolidBrush(Color.Blue), i * 38, r.Next(0, 15));
}
//生成干扰线条
Pen pen = new Pen(new SolidBrush(Color.Blue), 1);
for (int i = 0; i < 10; i++)
...{
g.DrawLine(pen, new Point(r.Next(0, 199), r.Next(0, 59)), new Point(r.Next(0, 199), r.Next(0, 59)));
}
b.Save(hc.Response.OutputStream, ImageFormat.Gif);
hc.Session[strIdentify] = s.ToString(); //先保存在Session中,验证与用户输入是否一致
hc.Response.End();
}
/**//// <summary>
/// 表示此类实例是否可以被多个请求共用(重用可以提高性能)
/// </summary>
public bool IsReusable
...{
get
...{
return true;
}
}
}
前台login.aspx使用
<img width="100px" id="ImgPwd" height="25px" src="ValidateImageHandler.ashx?" onclick= "this.src= 'ValidateImageHandler.ashx? ' + Math.random(); "alt ="图片看不清楚,请刷新页面"/>
在Page_Load中读 Session的值是不行的。原因是Page_Load先于验证码图片加载,这时Session还没有写入。在登录按钮的onclik事件中读取Session值应该可以吧,但为什么也不行呢?
使用<%Response.Write("验证码= "+Session["Identify"]);%> ,得到的是上一张验证码。
在http://blog.csdn.net/chengking/archive/2007/01/09/1475140.aspx这篇文章中,给了使用ASP.NET 2.0 HttpHandler实现生成图片验证码的例子,还是不明白如何判断验证码是否正确,
ValidateImageHandler.ashx
<%@ WebHandler Language="C#" Class="ValidateImageHandler" %>
using System;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
/**//// <summary>
/// ValidateImageHandler 生成网站验证码功能
/// </summary>
public class ValidateImageHandler : IHttpHandler, IRequiresSessionState
...{
int intLength = 5; //长度
string strIdentify = "Identify"; //随机字串存储键值,以便存储到Session中
public ValidateImageHandler()
...{
}
/**//// <summary>
/// 生成验证图片核心代码
/// </summary>
/// <param name="hc"></param>
public void ProcessRequest(HttpContext hc)
...{
//设置输出流图片格式
hc.Response.ContentType = "image/gif";
Bitmap b = new Bitmap(200, 60);
Graphics g = Graphics.FromImage(b);
g.FillRectangle(new SolidBrush(Color.YellowGreen), 0, 0, 200, 60);
Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel);
Random r = new Random();
//合法随机显示字符列表
string strLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
StringBuilder s = new StringBuilder();
//将随机生成的字符串绘制到图片上
for (int i = 0; i < intLength; i++)
...{
s.Append(strLetters.Substring(r.Next(0, strLetters.Length - 1), 1));
g.DrawString(s[s.Length - 1].ToString(), font, new SolidBrush(Color.Blue), i * 38, r.Next(0, 15));
}
//生成干扰线条
Pen pen = new Pen(new SolidBrush(Color.Blue), 1);
for (int i = 0; i < 10; i++)
...{
g.DrawLine(pen, new Point(r.Next(0, 199), r.Next(0, 59)), new Point(r.Next(0, 199), r.Next(0, 59)));
}
b.Save(hc.Response.OutputStream, ImageFormat.Gif);
hc.Session[strIdentify] = s.ToString(); //先保存在Session中,验证与用户输入是否一致
hc.Response.End();
}
/**//// <summary>
/// 表示此类实例是否可以被多个请求共用(重用可以提高性能)
/// </summary>
public bool IsReusable
...{
get
...{
return true;
}
}
}
前台login.aspx使用
<img width="100px" id="ImgPwd" height="25px" src="ValidateImageHandler.ashx?" onclick= "this.src= 'ValidateImageHandler.ashx? ' + Math.random(); "alt ="图片看不清楚,请刷新页面"/>
在Page_Load中读 Session的值是不行的。原因是Page_Load先于验证码图片加载,这时Session还没有写入。在登录按钮的onclik事件中读取Session值应该可以吧,但为什么也不行呢?
使用<%Response.Write("验证码= "+Session["Identify"]);%> ,得到的是上一张验证码。
相关文章推荐
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
- ASP.NET 2.0 HttpHandler实现生成图片验证码
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
- ASP.NET 2.0 HttpHandler实现生成图片验证码(转)
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)