asp.net写验证码
生成验证码与匹配验证码的服务端代码
<%@ WebHandler Language="C#" Class="ValidataeCodeHandler" %> using System;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging; public class ValidataeCodeHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{ public void ProcessRequest(HttpContext context)
{
//请求类型:获取验证码图片,匹配验证码
string type = context.Request["type"]; if (type == "math")
{
if (string.IsNullOrEmpty(context.Request["code"]))
context.Response.Write();
else if (string.IsNullOrEmpty("" + context.Session["yqcode" + context.Request["id"]]))
{
context.Response.Write();
}
else
{
if ("" + context.Session["yqcode" + context.Request["id"]] == context.Request["code"] + "")
context.Response.Write();
else
context.Response.Write(); }
}
else
{
context.Response.ContentType = "image/gif"; string validateCode = CreateValidateCode(context);//生成验证码
Bitmap bitmap = new Bitmap(imgWidth, imgHeight);//生成Bitmap图像
DisturbBitmap(bitmap); //图像背景
DrewValidateCode(bitmap, validateCode);//绘制验证码图像
bitmap.Save(context.Response.OutputStream, ImageFormat.Gif);//保存图像,等待输出 context.Response.Write(bitmap);
}
} // private int codeLen = 4;//验证码长度
private int fineness = ;//图片清晰度
private int imgWidth = ;//图片宽度
private int imgHeight = ;//图片高度
private string fontFamily = "Times New Roman";//字体名称
private int fontSize = ;//字体大小
//private int fontStyle = 0;//字体样式
private int posX = ;//绘制起始坐标X
private int posY = ;//绘制坐标Y
private string CreateValidateCode(HttpContext context) //生成验证码
{
string validateCode = "";
Random random = new Random();// 随机数对象
validateCode = random.Next(, ) + "";
//for (int i = 0; i < codeLen; i++)//循环生成每位数值
//{
// int n = random.Next(10);//数字
// validateCode += n.ToString();
//}
context.Session["yqcode" + context.Request["id"]] = validateCode;//保存验证码 这Session是在前台调用的。
return validateCode;// 返回验证码
} private void DisturbBitmap(Bitmap bitmap)//图像背景
{
Random random = new Random();//通过随机数生成
for (int i = ; i < bitmap.Width; i++)//通过循环嵌套,逐个像素点生成
{
for (int j = ; j < bitmap.Height; j++)
{
if (random.Next() <= this.fineness)
bitmap.SetPixel(i, j, Color.LightGray);
}
}
}
private void DrewValidateCode(Bitmap bitmap, string validateCode)//绘制验证码图像
{
Graphics g = Graphics.FromImage(bitmap);//获取绘制器对象
Font font = new Font(fontFamily, fontSize, FontStyle.Bold);//设置绘制字体
g.DrawString(validateCode, font, Brushes.Black, posX, posY);//绘制验证码图像
} public bool IsReusable
{
get
{
return false;
}
} }
手动刷新验证码
<script type="text/javascript">
//点击刷新验证码
function f_refreshtype() {
var Image1 = document.getElementById("valiCode");
if (Image1 != null) {
Image1.src = Image1.src + "?";
}
}
</script>
提交表单前ajax同步验证验证码是否正确
var urlCode = '/Handler/ValidataeCodeHandler.ashx?id=12&type=math&code=' + $.trim($("#yzcode").val());
//ajax同步请求
var mathresult = $.ajax({ type: "GET", url: urlCode, async: false }).responseText;
if (mathresult != 1) {
var Image1 = document.getElementById("valiCode");
if (Image1 != null) {
Image1.src = Image1.src + "?";
}
alert("验证码不匹配!");
return false;
}
<div class="line">
<span>验证码:</span><input value="" type="text" name="yzcode" id="yzcode" class="tong" />
<em><img id="valiCode" name="valiCode" onclick="f_refreshtype();" style="height:32px;" src="/Handler/ValidataeCodeHandler.ashx?id=12"/></em>
</div>
asp.net写验证码的更多相关文章
- ASP.NET MVC验证码演示(Ver2)
前一版本<ASP.NET MVC验证码演示>http://www.cnblogs.com/insus/p/3622116.html,Insus.NET还是使用了Generic handle ...
- 用ASP.Net写一个发送ICQ信息的程序
用ASP.Net写一个发送ICQ信息的程序 这里我给大家提供一个很实用的例子,就是在线发送ICQ信息.想一想我们在网页上直接给朋友发送ICQ信息,那是多么美妙的事情啊.呵呵,在吹牛啊,其实ICQ本来就 ...
- 【转载】Asp.Net生成图片验证码工具类
在Asp.Net应用程序中,很多时候登陆页面以及其他安全重要操作的页面需要输入验证码,本文提供一个生成验证码图片的工具类,该工具类通过随机数生成验证码文本后,再通过C#中的图片处理类位图类,字体类,一 ...
- asp.net 编写验证码
首先准备一个类来实现对验证码的绘制功能. createcode.cs using System; using System.Collections.Generic; using System.Linq ...
- ASP.NET实现验证码图片
新建一个checkcode.aspx文件,页面中不用写任何东西,在代码中,Page_Load中写入如下代码: string chkCode = string.Empty; int ix, ...
- ASP.NET生成验证码
首先,添加一个一般处理程序 注释很详细了,有不懂的欢迎评论 using System; using System.Collections.Generic; using System.Drawing; ...
- ASP.NET图形验证码的生成
效果: 调用方法: int[] r = QAPI.VerifImage.RandomList();//取得随机数种子列 );//产生验证码字符 pictureBox1.Image = QAPI.Ver ...
- 用Asp.net写自己的服务框架
阅读目录 开始 理解Asp.net管线 HttpHandler HttpModule 关于Content-Encoding的解释 选 HttpHandler 还是 HttpModule ? 看不见的性 ...
- ASP.NET——生成验证码
实现:随机生成四位数字的验证码,点击验证码可无刷新生成新的验证码,最后点击按钮进行检验 PS:本实例使用UpdatePanel实现无刷新. 前台代码: <asp:ScriptManager ID ...
随机推荐
- 20. Candy && Gas Station
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- win10内网外网智能访问
当电脑同时连接有线和WiFi时(有线连接为内网,WiFi为外网),会出现内网和外网内容无法同时访问的情况. 本方法实现内网和外网的同时访问. 第一步: 输入指令 “route print ” 查看路由 ...
- Java运算符的优先级(从高到低)
运算符的优先级(从高到低) 优先级 描述 运算符 1 括号 ().[] 2 正负号 +.- 3 自增自减,非 ++.--.! 4 乘除,取余 *./.% 5 加减 +.- 6 移位运算 << ...
- maximo功能修改笔记
经过前几次的简单的修改系统功能,对maximo的bean开发已经有了一定了解,现在是耗时近两个礼拜来修改了一项系统功能,所用到的知识 Bean Fld, 下面我认真总结修改功能过程中的学到的知识: 目 ...
- 第五百七十七天 how can I 坚持
今天看了个电影<七月与安生>,挺不错,周冬雨,马思纯,然后就突然有了个想法,过年不回家了,去趟拉萨,或许只是想想吧,不知道有没有勇气去啊,何况是自己一个人,但是又想,旅行要是没点冒险的话, ...
- FireDAC 超时
FireDAC 超时 Timeout expired 在Win10 正常. 在Win7 CB的DLL 正常,Delphi的DLL怎么会超时呢??? 果然是连接字符串错了.改为正确的就连接正常了!
- Spring项目解决Post乱码
Java EE解决Post乱码:在web.xml中加入: <filter> <filter-name>encodingFilter</filter-name> &l ...
- JQuery之正则表达式
1.定义正则表达式 /.../ 用于定义正则表达式 /.../g 表示全局匹配 /.../i 表示不区分大小写 /.../m 表示多行匹配 2.匹配正则表达式 非全局模式,不分组 var patte ...
- Error : Must specify a primary resource (JAR or python or R file)
spark-submit 报错:must specify resource 取消关注 | 1 ... 我的submit.sh内容: /bin/spark-submit \ --class abc.pa ...
- C++之jsoncpp学习
最新由于客户端要用到jsoncpp,所以自己也跟着项目的需求学了一下jsoncpp.以前没用过xml,但是感觉接触json后,还蛮好用的. 参考地址 http://jsoncpp.sourceforg ...