asp.net 生成验证码问题 

.添加一个.ashx文件

<%@ WebHandler Language="C#" class="CheckCode" %>

using System;
using System.Web;
using System.Drawing; public class CheckCode : IHttpHandler { public void ProcessRequest(HttpContext context)
{
string g = GenerateCheckCode();
CreateCheckCodeImage(g); } public bool IsReusable
{
get
{
return false;
}
} private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty; System.Random random = new Random(); for (int i = ; i < ; i++)
{
number = random.Next(); if (number % == )
//生成'0'-'9'字符
code = (char)('' + (char)(number % ));
else
//生成'A'-'Z'字符
code = (char)('A' + (char)(number % )); checkCode += code.ToString();
} HttpContext.Current.Response.Cookies.Add(new HttpCookie("checkcode", checkCode)); return checkCode;
//两个字符相加等于=asicc码加
} private void CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return;
//(int)Math.Ceiling((checkCode.Length * 12.5)),22
System.Drawing.Bitmap image = new System.Drawing.Bitmap(, );
Graphics g = Graphics.FromImage(image); try
{
//生成随机生成器
Random random = new Random(); //清空图片背景色
g.Clear(Color.White); //画图片的背景噪音线
for (int i = ; i < ; i++)
{
int x1 = random.Next(image.Width+);
int x2 = random.Next(image.Width+);
int y1 = random.Next(image.Height+);
int y2 = random.Next(image.Height+); Pen p = new Pen(Color.Gray, );
g.DrawLine(p, x1, y1, x2, y2);
} Font font = new System.Drawing.Font("Arial", , (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(, , image.Width, image.Height), Color.Blue, System.Drawing.Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, , ); //画图片的前景噪音点
for (int i = ; i < ; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height); image.SetPixel(x, y, System.Drawing.Color.FromArgb(random.Next()));
} //画图片的边框线
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Silver), , , image.Width - , image.Height - ); System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/jpg";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
} } .在用到验证码的界面添加一个img控件,并且src属性指向.ashx文件 <img alt="看不清,换一张" src="CheckCode.ashx" onclick="this.src='CheckCode.ashx?abc='+Math.random()" /> .提交后先判断验证码是否正确 //检查验证码是否正确
if(this.txt_checkcode.Text.ToLower ()!=this.Request.Cookies["checkcode"].Value.ToLower () )
{
//写脚本提示
if( this.ClientScript.IsStartupScriptRegistered ("checkcode")==false)
{
this.ClientScript.RegisterStartupScript(this.GetType (),"checkcode","<script>alert('验证码错误!');</script>");
}
return;
}

asp.net验证码的更多相关文章

  1. ASP.net 验证码(C#) MVC

    ASP.net 验证码(C#) MVC http://blog.163.com/xu_shuhao/blog/static/5257748720101022697309/ 网站添加验证码,主要为防止机 ...

  2. MVC的验证(模型注解和非侵入式脚本的结合使用) .Net中初探Redis .net通过代码发送邮件 Log4net (Log for .net) 使用GDI技术创建ASP.NET验证码 Razor模板引擎 (RazorEngine) .Net程序员应该掌握的正则表达式

    MVC的验证(模型注解和非侵入式脚本的结合使用)   @HtmlHrlper方式创建的标签,会自动生成一些属性,其中一些属性就是关于验证 如图示例: 模型注解 通过模型注解后,MVC的验证,包括前台客 ...

  3. Asp.Net验证码2

    using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System. ...

  4. 关于 ASP.NET 验证码

    Session["CheckCode"] 这个..不懂神马意思.. .创建一个用户控件 用户名:TextBox 密码: TextBox 验证码:TextBox 验证码图片 < ...

  5. ASP.NET 验证码 不同浏览器 不刷新问题

    具体为什么不刷新是缓存机制不同,验证码图片的src或ImageUrl的获取是来自一个文件,由于连接地址没变所以不同内核浏览器有的会认为源没有变,解决办法就是在连接后面加上一个随机参数如可以用JS的Ma ...

  6. asp.net验证码及怎么获取里面的数值(整合)

    一.ASP.Net的验证码的作用 对于一个预防攻击的web表单来讲,验证码通常是一个常见的措施.因为如果对于一些public区域的页面内容来讲,譬如一个登录表单,如果没有必要的安全措施,很可能遭到模拟 ...

  7. ASP.NET——验证码的制作

            我们在登陆站点,发表博客或者提交评论的时候,常常会遇到填写验证码这一项,当时感觉挺奇妙的样子,最终在牛腩新闻公布系统里接触到了,在这里小小的总结下.         用到的东东有三个: ...

  8. asp.net 验证码技术

    网站验证码是一种很常用的技术.下面我介绍下技术上是如何实现的. 验证码是一张图片.我们需要在前台代码中写一段<img>,src指向一张页面(ValidateImage.aspx). < ...

  9. asp.net验证码的编写

    很多时候我们在登录什么网站的时候,除了需要什么用户名和密码之外,有的还需要验证码那么在asp.net中这个验证码如何编写和设计,今天我就来给大家说一下: 首先创建一个页面名字随便起一个,我们这里叫做C ...

随机推荐

  1. Linux中利用extundelete恢复误删除的数据

    利用extundelete工具恢复磁盘误删除的数据 原理: 简单介绍下关于inode的知识.在Linux下可以通过"ls -id"命令来查看某个文件或者目录的inode值,例如查看 ...

  2. mac上配置java开发环境

    项目在mac上跑起来的步骤: 1. 访问,https://brew.sh/  装上这个然后  brew install git  brew install maven, settings.xml需要放 ...

  3. test pthread code

    #include <iostream> #include <pthread.h> using namespace std; ; void * add(void *); pthr ...

  4. 枚举Enum 的常用方法

    一.枚举介绍 通常定义常量方法和枚举定义常量方法区别 public class State { public static final int ON = 1; public static final ...

  5. python ----元组方法以及修改细节

    元组的一级元素不可修改 #元组,有序 tu = (111,"alex",(11,22),[(33,44)],True,33,44,) v = tu[3][0][0] print(v ...

  6. Learn how to use git

    Git配置 $ git config --global user.name "Your Name" $ git config --global user.email "e ...

  7. Binary Tree Path Sum

    Given a binary tree, find all paths that sum of the nodes in the path equals to a given number targe ...

  8. php 安装redis

    https://www.cnblogs.com/yuuje/p/8243234.html

  9. R语言安装xlsx包,读入excel表格

    开学的时候,男神给了数据(.xlsx格式)让用R语言分析分析,作为编程小白,读了一天都没读近R,更别提如何分析了. 现在小伙伴们都喜欢读txt 和csv格式的,好多xlsx的表格读不进R,将xlsx格 ...

  10. ACM-ICPC 2018 南京赛区网络预赛B

    题目链接:https://nanti.jisuanke.com/t/30991 Feeling hungry, a cute hamster decides to order some take-aw ...