·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> 关于在一般处理程序中实现基础验证码

关于在一般处理程序中实现基础验证码

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

验证码是一个图片是动态生成的,一般的验证码保存在服务器中。

要在一般处理程序中使用session必须实现System.Web.SessionState.IRequiresSessionState 接口不然会出现找不到session的错误。

public void PRocessRequest (HttpContext context) {
context.Response.ContentType = "image/JPEG";//这里要改正格式以前为(text/plan)
//创建一个位图确定图片的大小(图片的大小)
using (System.Drawing.Bitmap bitmip=new System.Drawing.Bitmap(100,50))
{ //创建一个画布画出刚刚创建的图片
using (System.Drawing.Graphics g=System.Drawing.Graphics.FromImage(bitmip))
{
//随即创建int类型的验证码
Random rand = new Random();
int code = rand.Next();
string strcode = code.ToString();
//把产生的验证码保存到session中
HttpContext.Current.Session["code"] = strcode;
// 设置画布中的内容,字体大小,字体颜色
g.DrawString(strcode, new System.Drawing.Font("宋体", 12), System.Drawing.Brushes.Yellow, new System.Drawing.PointF(0, 0));//font是要回收的
//保存
bitmip.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}

在aspx页面调用session的值与用户提交的比较。