生成验证码与匹配验证码的服务端代码

<%@ 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写验证码的更多相关文章

  1. ASP.NET MVC验证码演示(Ver2)

    前一版本<ASP.NET MVC验证码演示>http://www.cnblogs.com/insus/p/3622116.html,Insus.NET还是使用了Generic handle ...

  2. 用ASP.Net写一个发送ICQ信息的程序

    用ASP.Net写一个发送ICQ信息的程序 这里我给大家提供一个很实用的例子,就是在线发送ICQ信息.想一想我们在网页上直接给朋友发送ICQ信息,那是多么美妙的事情啊.呵呵,在吹牛啊,其实ICQ本来就 ...

  3. 【转载】Asp.Net生成图片验证码工具类

    在Asp.Net应用程序中,很多时候登陆页面以及其他安全重要操作的页面需要输入验证码,本文提供一个生成验证码图片的工具类,该工具类通过随机数生成验证码文本后,再通过C#中的图片处理类位图类,字体类,一 ...

  4. asp.net 编写验证码

    首先准备一个类来实现对验证码的绘制功能. createcode.cs using System; using System.Collections.Generic; using System.Linq ...

  5. ASP.NET实现验证码图片

    新建一个checkcode.aspx文件,页面中不用写任何东西,在代码中,Page_Load中写入如下代码: string chkCode = string.Empty;        int ix, ...

  6. ASP.NET生成验证码

    首先,添加一个一般处理程序 注释很详细了,有不懂的欢迎评论 using System; using System.Collections.Generic; using System.Drawing; ...

  7. ASP.NET图形验证码的生成

    效果: 调用方法: int[] r = QAPI.VerifImage.RandomList();//取得随机数种子列 );//产生验证码字符 pictureBox1.Image = QAPI.Ver ...

  8. 用Asp.net写自己的服务框架

    阅读目录 开始 理解Asp.net管线 HttpHandler HttpModule 关于Content-Encoding的解释 选 HttpHandler 还是 HttpModule ? 看不见的性 ...

  9. ASP.NET——生成验证码

    实现:随机生成四位数字的验证码,点击验证码可无刷新生成新的验证码,最后点击按钮进行检验 PS:本实例使用UpdatePanel实现无刷新. 前台代码: <asp:ScriptManager ID ...

随机推荐

  1. Navicat链接Oracle提示ORA-12737

    ORA-12737: Instant Client Light: unsupported server character set string Cause: The character set sp ...

  2. IIS7 IIS7.5 IIS8.5 HTTP 错误 500.19 – Internal Server Error解决方案小记

    今天配置IIS(win8.1 IIS8.5)的web.config出现如下错误: HTTP 错误 500.19 – Internal Server Error无法访问请求的页面,因为该页的相关配置数据 ...

  3. TortoiseSVN 版本回滚

    尝试用TortoiseSVN进行版本回滚,回滚到的版本和实际的内容有出入,可能是点了太多次给点乱了,囧~ 不过发现一个比较靠谱的方法,如下: 右键点击文件TortoiseSVN->showlog ...

  4. Oracle 表分区

    从以下几个方面来整理关于分区表的概念及操作: 表空间及分区表的概念 表分区的具体作用 表分区的优缺点 表分区的几种类型及操作方法 对表分区的维护性操作 1.表空间及分区表的概念 表空间: 是一个或多个 ...

  5. c#基础-oop(面向对象理解)

    OOP-面向对象 封装,继承多态 一个桌子,用面向对象来描述一下它这个桌子项目 定义桌子类 对象:桌子 桌子的属性:名字,材质,体积 桌子的方法;放东西(方法) 现在桌子要放书,放花瓶,放文件(这里就 ...

  6. 正确停止kafka的方法

    kill -15 pid 即: kill SIGNTERM pid 不要使用kill -9. kill -15会触发调用shutdownHook的run方法,从而可以执行关闭服务器的时候一些必要代码. ...

  7. C++中使用初始化列表比在构造函数中对成员变量赋值更高效

    这是在面试中遇到的一个问题,没有答出来,后来上网上查了一些资料,终于弄明白了: 一.首先c++标准规定成员变量必须在调用构造函数前进行初始化(这一点很重要) 二.如果我们在构造函数中对成员变量进行初始 ...

  8. VC MFC在CMFCToolBar工具栏中加入组合框

    如何在CMFCToolBar工具栏中加入组合框等控件,且先看在线MSDN上怎么说的: 要增加一个组合框,需要完成以下步骤: 1.在工具栏资源中,增加一个对应ID资源号的按钮. 2.在主框架(mainf ...

  9. 给Xcode配置VVDocumenter-Xcode-master,注释插件

    1.      去github上下载     https://github.com/onevcat/VVDocumenter-Xcode   . 2.      打开工程,command+B 编译成功 ...

  10. spi controller

    http://blog.csdn.net/droidphone/article/details/24353293 http://www.china-cpu.com/supports/article/0 ...