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

<%@ 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. php加密解密0x数组

    <?php //加密字符串 $str='hello world'; $arr=str_split($str); $arr1=array(); foreach($arr as $v){ $ordv ...

  2. 初学javaweb,远离各自框架

    OSCHINA 软件库有一个分类--Web框架,该分类中包含多种编程语言的将近500个项目. Web框架是开发者在使用某种语言编写Web应用服务端时关于架构的最佳实践.很多Web框架是从实际的Web项 ...

  3. ssh IP打通,hadoop启动失败

    ssh ip 无密码打通,hadoop启动失败 报错为:host'主机名' can't be established. 纠结了接近一个多小时 之后必须ssh 主机名 , yes一下,发现hadoop能 ...

  4. 第五百八十六天至第六百零五天 how ccan I 坚持

    考研中,勿扰... 我是个逗比,哈哈. 时间不够用了呢,哎.

  5. Spark Idea Maven 开发环境搭建

    一.安装jdk jdk版本最好是1.7以上,设置好环境变量,安装过程,略. 二.安装Maven 我选择的Maven版本是3.3.3,安装过程,略. 编辑Maven安装目录conf/settings.x ...

  6. 关于delphi 中 Sender的学习

    sender是 事件的触发者,我发现所有的组件的事件 基本上都是 传Sender. 示例效果图: 代码: 接着来,既然TButton是个类,且publish哪里有事件,我们也可以看看这个事件的原型.

  7. Sql 数据引擎中删除用户名、密码信息

    SQl版本:Microsoft SQL Server 2008 R2 系统:Windows Server 2008 R2 Enterprise 删除文件为:SqlStudio.bin 删除星系路径:C ...

  8. Python学习笔记(二)基本语法

    Class 2 一.交互式编程 交互式编程不需要创建脚本文件,是通过 Python 解释器的交互模式进来编写代码. linux上你只需要在命令行中输入 Python 命令即可启动交互式编程,如下图: ...

  9. jsTree搜索排序向上向下

    var _node = null, _all_match = 0, _current_match = 0; $(document).ready(function() { $('#area_settin ...

  10. Android 下拉刷新框架实现

    原文地址:http://blog.csdn.net/leehong2005/article/details/12567757 前段时间项目中用到了下拉刷新功能,之前在网上也找到过类似的demo,但这些 ...