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 ...
随机推荐
- [CF442B] Andrey and Problem (概率dp)
题目链接:http://codeforces.com/problemset/problem/442/B 题目大意:有n个人,第i个人出一道题的概率是pi,现在选出一个子集,使得这些人恰好出一个题的概率 ...
- Django model中常见Field types , Field options
AutoField :自增,数据库 BooleanField:布尔型 CharField:字符型 DateField:日期,datetime.date类的实例.有两个常用的option,auto_no ...
- java日期比较,日期计算
版权声明:本文为楼主原创文章,未经楼主允许不得转载,如要转载请注明来源. 都是常用的日期之间的比较方法,供以后参考. 热身:获取当前时间 SimpleDateFormat df = new Simpl ...
- Python:if __name__ == '__main__'
很多模块里都会看到这句话,一般用于模块自测时使用. 所有的模块都有一个内置属性 __name__. 一个模块的 __name__ 的值取决于您如何应用模块. 一个Python文件有两种使用方式,直接使 ...
- nginx 日志分割
利用 crontab + shell 来实现nginx的 access log 按天切割,便于统计.具体实现如下: shell: #! /bin/sh NGINX_DIR=/data/apps/ngi ...
- maven 打包 spring 项目
在程序中使用到了springframework控件(主要是为了使用Mybatis-spring操作数据库,省事). 使用maven管理项目的构建,现在需要生成一个jar包,包含所有依赖的jar包,并可 ...
- 比较一下Linux下的Epoll模型和select模型的区别
一. select 模型(apache的常用) 1. 最大并发数限制,因为一个进程所打开的 FD (文件描述符)是有限制的,由 FD_SETSIZE 设置,默认值是 1024/2048 ,因此 Sel ...
- 基于percona 5.7的xtrabackup实践
环境: centerOS7 percona 5.7 xtrabackup 2.4(5.7只支持2.4已上的版本) 第一步: 安装xtraback ...
- [转载] 1. JebAPI 之 jeb.api
本文转载自: https://www.zybuluo.com/oro-oro/note/142707 JEB API 官方地址:https://www.pnfsoftware.com/apidoc/ ...
- [转载] Android Studio 上第一个 Xposed 模块
本文转载自: http://www.open-open.com/lib/view/open1451364108964.html 环境: 已root手机一枚 Android Studio一枚 官方文档参 ...