数学运算表达式形式的验证码 C# Asp.Net
来源:互联网 发布:114la源码 编辑:程序博客网 时间:2024/05/16 06:48
http://www.cnblogs.com/sunnylike/archive/2009/04/08/1431710.html
/*---------------------------------------
* Author: DeltaCat (三角猫)
*
* http://www.zu14.cn (真有意思网)
*
* Date: 2008/11/22
*
* 转载请保留此信息
--------------------------------------*/
/// <summary>
/// 数学算式的验证码
/// </summary>
public sealedclass MathVerifyCode
{
#region 生成图片
/// <summary>
/// 输出验证码表达式到浏览器
/// </summary>
/// <param name="context">httpcontext</param>
/// <param name="sessionKey">保存运算值的SESSION的KEY</param>
public void OutputImage(System.Web.HttpContext context,string sessionKey)
{
int mathResult = 0;
string expression = null;
Random rnd = new Random();
////生成3个10以内的整数,用来运算
int operator1 = rnd.Next(0, 10);
int operator2 = rnd.Next(0, 10);
int operator3 = rnd.Next(0, 10);
////随机组合运算顺序,只做 + 和 * 运算
switch (rnd.Next(0, 3))
{
case 0:
mathResult = operator1 + operator2 * operator3;
expression = string.Format("{0} + {1} * {2} = ?",operator1, operator2,operator3);
break;
case 1:
mathResult = operator1 * operator2 + operator3;
expression = string.Format("{0} * {1} + {2} = ?",operator1, operator2,operator3);
break;
default:
mathResult = operator2 + operator1 * operator3;
expression = string.Format("{0} + {1} * {2} = ?",operator2, operator1,operator3);
break;
}
using (Bitmap bmp = new Bitmap(150, 25))
{
using (Graphics graph = Graphics.FromImage(bmp))
{
graph.Clear(Color.FromArgb(232, 238, 247)); ////背景色,可自行设置
////画噪点
for (int i = 0; i <= 128; i++)
{
graph.DrawRectangle(
new Pen(Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255))),
rnd.Next(2, 128),
rnd.Next(2, 38),
1,
1);
}
////输出表达式
for (int i = 0; i < expression.Length; i++)
{
graph.DrawString(expression.Substring(i, 1),
new Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold),
new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(128), rnd.Next(255))),
5 + i * 10,
rnd.Next(1, 5));
}
////画边框,不需要可以注释掉
graph.DrawRectangle(new Pen(Color.Firebrick), 0, 0, 150 - 1, 25 - 1);
}
context.Session[sessionKey] = mathResult; ////将运算结果存入session
////禁用缓存
DisableHttpCache(context);
////输出图片到浏览器,我采用的是 gif 格式,可自行设置其他格式
context.Response.ContentType = "image/gif";
bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
context.Response.End();
}
}
#endregion
/// <summary>
/// 禁用缓存
/// </summary>
/// <param name="context">httpcontext</param>
private staticvoid DisableHttpCache(System.Web.HttpContext context)
{
////清除http缓存
context.Response.ClearHeaders();
context.Response.ClearContent();
////禁用http缓存
//// http 1.1
context.Response.AddHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT");
context.Response.AddHeader("Cache-Control", "no-store, no-cache, max-age=0, must-revalidate");
//// http 1.0
context.Response.AddHeader("Pragma", "no-cache");
}
}
注: 如果觉得验证码太模糊,可以将画噪点的地方调整为下面的:
for (int i = 0; i <= 128; i++) { graph.DrawRectangle( new Pen(Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255))), (float)rnd.Next(2, 128), (float)rnd.Next(2, 38), 0.5F, //噪点的粒度 0.5F);//噪点的粒度,可以调节这两个值,到认为自己满意 }
使用说明:
介绍两种使用方式:
——————————————————————–
1. 使用普通的 aspx 页面承载输出 image
2. 使用 HttpHandler文件,就是 ashx文件承载输出image
——————————————————————–
推荐后者,使用方法如下:
——————————————————————–
1. 使用普通的 aspx 页面承载输出 image
2. 使用 HttpHandler文件,就是 ashx文件承载输出image
——————————————————————–
推荐后者,使用方法如下:
aspx页面
//新建一个ASPX页面,例如: mvc.aspx//在 Page_Load 事件里,写入 protected void Page_Load(object sender, EventArgs e) { MathVerifyCode mVC = new MathVerifyCode(); mVC.OutputImage(HttpContext.Current, "mvc"); //mvc是保存验证码结果的SESSION的key }
ashx方式
//新建一个 ashx 页面,例如: Mvc.ashx//记得此ashx文件的类,一定要继承 System.Web.SessionState.IRequiresSessionState 才可以使用sessionpublic class Mvc : IHttpHandler, System.Web.SessionState.IRequiresSessionState{ public void ProcessRequest (HttpContext context) { MathVerifyCode mvc = new MathVerifyCode(); mvc.OutputImage(context, "mvc"); } public bool IsReusable { get { return true; } }}
在需要显示验证码的地方,加入:
<img src="mvc.aspx" alt=""/>或者<img src="mvc.ashx" alt=""/>
0 0
- 数学运算表达式形式的验证码 C# Asp.Net
- 数学运算表达式形式的验证码 C# Asp.Net
- asp.net数学运算表达式形式的验证码
- 数学运算表达式形式的验证码
- asp.net C# 验证电子邮件的格式(正则表达式)
- asp.net C# 验证控件常用的正则表达式
- asp.net 验证的正则表达式
- ASP.NET生成验证码的三种形式【附源码下载】
- c# asp.net实现验证码
- c# asp.net实现验证码
- ASP.NET C#验证码控件使用
- asp.net中用C#生产验证码
- asp.net,C#验证码使用
- asp.net字符串的数学表达式计算结果
- C#数学运算表达式解释器
- asp.net验证正则表达式
- asp.net 验证正则表达式
- asp.net 验证正则表达式
- csdn博客从今天开始正式开启继续写博客模式
- uva--10130SuperSale +dp
- 【转】C#中 TreeView如何获取某一节点下的所有子节点
- SQL SERVER 2005/08 数据库置疑或挂起恢复
- Android系统启动过程
- 数学运算表达式形式的验证码 C# Asp.Net
- 如何解决WebSocket-Sharp在Unity3d中无法跨与访问不同网断服务器
- STM32使用串口中断发送和接受数据
- N-Queens II
- Android中适用于ListView、GridView等组件的通用Adapter (转)
- Git安装、配置、上传公钥、测试登录及clone库(用户教程)
- 用jwplayer播放视频
- netduino固件升级方法
- Cookie/Session机制详解