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

<%@ 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. No.1__C#

    这是第一篇C#的日记,到现在为止已经学习了一个礼拜的C#了.由于是实习中才开始学习,所以这次不准备像在大学学习那样,拿着课本划重点,背概念.这应当是一门实践的课程,应该一边编程,一边学.这是到公司第一 ...

  2. python_day2

    一.字符串的基本使用 #!/usr/bin/env python #!-*- coding:utf-8 -*- #!/usr/bin/env python  指定解释器为python abc='hel ...

  3. gcc与makefile编译 BY 四喜三顺

    gcc编译控制过程:(假设源代码为a.c)(1)源文件到预处理文件:    gcc -E -o a.cxx a.c    a.cxx显示调用哪些头文件(2)生成汇编代码:              g ...

  4. uploadify图片上传发生Security Error

    今天在使用uploadify进行图片上传的时候出现security error.其根本原因是flash跨域问题.主要原因是因为配了两个域名www.ttyouni.com 和 ttyouni.com 在 ...

  5. .net实现调用本地exe等应用程序的办法总结

    根据客户需求用户要实现在一个BS系统上打开本地的一应用程序,在网上查了好多资料再加上自己的各种测试,到最后功能是实现了,只不过还存在一些问题,接下来会先把各种方法一一列举出来 1.先写最终测试通过的这 ...

  6. java基础回顾(八)——Queue

    今天回顾了下关于Queue的一些相关知识 我们可以看到,Deque也是一个接口,它继承了Queue的接口规范.其中LinkedList和ArrayDeque都是实现Deque接口,所以,可以说他们俩都 ...

  7. EXCL poi导入

    public static void importExcel2(File file) throws Exception { InputStream is = new FileInputStream(f ...

  8. http的响应对象

    Servlet 服务器 HTTP 响应 正如前面的章节中讨论的那样,当一个 Web 服务器响应一个 HTTP 请求时,响应通常包括一个状态行.一些响应报头.一个空行和文档.一个典型的响应如下所示: H ...

  9. 移动端自动化环境搭建-Appium for Windows的安装

    安装Appium for Windows版 A.安装依赖 appium就是我们做移动端自动化测试主要的软件 B.安装过程

  10. POJ(3468)

    A Simple Problem with Integers http://poj.org/problem?id=3468 #include <stdio.h> struct node { ...