验证码的作用:

几年前,大部分网站、论坛之类的是没有验证码的,因为对于一般用户来说验证码只是增加了用户的操作,降低了用户的体验。但是后来各种灌水机器人、投票机器人、恶意注册机器人层出不穷,大大增加了网站的负担同时也给网站数据库带来了大量的垃圾数据。为了防止各种机器人程序的破坏,于是程序员想出了只有人眼能够识别的,程序不容易识别的验证码!

验证码是一个图片,将字母、数字甚至汉字作为图片的内容,这样一张图片中的内容用人眼很容易识别,而程序将无法识别。在进行数据库操作之前(比如登录验证、投票、发帖、回复、注册等等)程序首先验证客户端提交的验证码是否与图片中的内容相同,如果相同则进行数据库操作,不同则提示验证码错误,不进行数据库操作。这样各种机器人程序就被拒之门外了!

但是随着计算机科学的发展,模式识别等技术越来越成熟,于是编写机器人程序的家伙可以通过程序将直接写在图片中的内容识别出来,然后提交到服务器,这样验证码将形同虚设。为了防止机器人程序的识别,验证码的图片生成也不断在发展,加入干扰点、干扰线,文字变形、变换角度位置,颜色不同……各种防止计算机识别的技术也应用到验证码中。就在这两种技术的竞争中,于是便形成了我们现在看到的验证码,已经有很多人在抱怨“这是什么验证码哦,人眼都分辨不清楚是什么”,一切也是无奈。

了解了验证码的作用,下面写一个简单的验证码生成及使用的实例

先建个页面用来展示验证码和判断验证码输入是否正确

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
//点击切换验证码
function f_refreshtype() {
var Image1 = document.getElementById("img");
if (Image1 != null) {
Image1.src = Image1.src + "?";
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td>
<img src="png.aspx" id="img" onclick="f_refreshtype()" />
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="确定" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

此页面后台对验证码进行验证

 protected void Page_Load(object sender, EventArgs e)
{
//生成的验证码被保存到session中
if (Session["CheckCode"] != null)
{
string checkcode = Session["CheckCode"].ToString();
if (this.TextBox1.Text == checkcode)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('验证码输入正确!')", true);
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('验证码输入错误!')", true);
}
} }

生成验证码页面png.aspx

  protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CreateCheckCodeImage(GenerateCheckCodes());
}
}
public void ShowAuthCode(Stream stream, out string code)
{
Random random = new Random();
code = random.Next(, ).ToString(); Bitmap bitmap = CreateAuthCode(code);
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Gif);
} private string GenerateCheckCodes(int iCount)
{
int number;
string checkCode = String.Empty;
int iSeed = DateTime.Now.Millisecond;
System.Random random = new Random(iSeed);
for (int i = ; i < iCount; i++)
{
number = random.Next();
checkCode += number.ToString();
}
Session["CheckCode"] = checkCode;
return checkCode;
} private Bitmap CreateAuthCode(string str)
{
Font fn = new Font("宋体", );
Brush forecolor = Brushes.Black;
Brush bgcolor = Brushes.White;
PointF pf = new PointF(, );
Bitmap bitmap = new Bitmap(, );
Rectangle rec = new Rectangle(, , , );
Graphics gh = Graphics.FromImage(bitmap);
gh.FillRectangle(bgcolor, rec);
gh.DrawString(str, fn, forecolor, pf);
return bitmap;
} private void CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return;
int iWordWidth = ;
int iImageWidth = checkCode.Length * iWordWidth;
Bitmap image = new Bitmap(iImageWidth, );
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);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
} //画图片的背景噪音线
for (int i = ; i < ; i++)
{
int x1 = ;
int x2 = image.Width;
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
if (i == )
{
g.DrawLine(new Pen(Color.Gray, ), x1, y1, x2, y2);
} } for (int i = ; i < checkCode.Length; i++)
{ string Code = checkCode[i].ToString();
int xLeft = iWordWidth * (i);
random = new Random(xLeft);
int iSeed = DateTime.Now.Millisecond;
int iValue = random.Next(iSeed) % ;
if (iValue == )
{
Font font = new Font("Arial", , (FontStyle.Bold | System.Drawing.FontStyle.Italic));
Rectangle rc = new Rectangle(xLeft, , iWordWidth, image.Height);
LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.Red, 1.5f, true);
g.DrawString(Code, font, brush, xLeft, );
}
else if (iValue == )
{
Font font = new System.Drawing.Font("楷体", , (FontStyle.Bold));
Rectangle rc = new Rectangle(xLeft, , iWordWidth, image.Height);
LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.DarkRed, 1.3f, true);
g.DrawString(Code, font, brush, xLeft, );
}
else if (iValue == )
{
Font font = new System.Drawing.Font("宋体", , (System.Drawing.FontStyle.Bold));
Rectangle rc = new Rectangle(xLeft, , iWordWidth, image.Height);
LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Green, Color.Blue, 1.2f, true);
g.DrawString(Code, font, brush, xLeft, );
}
else if (iValue == )
{
Font font = new System.Drawing.Font("黑体", , (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Bold));
Rectangle rc = new Rectangle(xLeft, , iWordWidth, image.Height);
LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.Green, 1.8f, true);
g.DrawString(Code, font, brush, xLeft, );
}
}
//////画图片的前景噪音点
//for (int i = 0; i < 8; i++)
//{
// int x = random.Next(image.Width);
// int y = random.Next(image.Height);
// image.SetPixel(x, y, Color.FromArgb(random.Next()));
//}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), , , image.Width - , image.Height - );
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}

.net验证码生成及使用的更多相关文章

  1. php 图片验证码生成 前后台验证

    自己从前一段时间做了个php小项目,关于生成图片验证码生成和后台的验证,把自己用到的东西总结一下,希望大家在用到相关问题的时候可以有一定的参考性. 首先,php验证码生成. 代码如下: 1.生成图像代 ...

  2. Atitit 图片 验证码生成attilax总结

    Atitit 图片 验证码生成attilax总结 1.1. 图片验证码总结1 1.2. 镂空文字  打散 干扰线 文字扭曲 粘连2 1.1. 图片验证码总结 因此,CAPTCHA在图片验证码这一应用点 ...

  3. ASP.NET验证码生成与识别

    一般验证码页面只输出一个图片而不进行其他业务处理,所以验证码一般放在一般处理程序(httpHandler)页面中,而如果将验证码生成代码放到一般处理程序中,要将生成验证码保存在Session中,这里我 ...

  4. ajax原理,验证码生成原理

    什么是ajax AJAX:”Asynchronous JavaScript and XML” 中文意思:异步JavaScript和XML 指一种创建交互式网页应用的网页开发技术.   不是指一种单一的 ...

  5. 验证码生成-->漂亮啊

    验证码不用输出太多的HTML代码,直接创建一个一般处理程序,直接上代码 public class VCode : IHttpHandler { HttpContext context = null; ...

  6. Web---图片验证码生成教程详解-从简单到复杂-从本地到前后台

    首先,我们先来看本地如何生成图片验证码的,再来写输出到网页的验证码如何实现. 先来看最简单的-实现的功能是,将一个字符串变成图片写入到文件中 实现代码: package cn.hncu.img; im ...

  7. 利用谷歌 kaptcha 进行验证码生成

    package main.com.smart.controller; import com.google.code.kaptcha.Producer; import main.com.smart.ut ...

  8. 轻量级验证码生成插件webutil-licenseImage

    轻量级验证码生成插件webutil-licenseImage源码与实例应用   webutil-licenseImage 插件内置4种验证码样式,支持用户扩展.自定义样式实现简单验证码. 源码脱管地址 ...

  9. JAVA 验证码生成(转)

    最近做了一下验证码的功能,网上找了一篇还不错,引用下:http://blog.csdn.net/ruixue0117/article/details/22829557 这篇文章非常好,但是web和js ...

随机推荐

  1. C#对数组去重

    #region ArrayList的示例应用 /// 方法名:DelArraySame /// 功能: 删除数组中重复的元素 /// </summary> /// <param na ...

  2. 黄聪:wordpress前台自定义用户,调用wp_editor上传附件提示【抱歉,出于安全的考虑,不支持此文件类型】错误。

    1.直接禁用文件类型检测,在wp-config.php文件中,添加这样一句代码define('ALLOW_UNFILTERED_UPLOADS', true); 2.在functions.php里面, ...

  3. 优化studio的速度

    随着Android Studio开发工具的逐渐成熟,越来越多的程序员选择这种IDE工具来进行开发,但是android studio在使用过程中有时候会出现卡顿问题.在赶项目的时候,遇到这类问题最是苦恼 ...

  4. ndk android studio万年坑

    先说javah万年坑 javah语法: Usage: javah [options] <classes> where [options] include: -o <file>  ...

  5. 里德九步审讯法 z

    在现实生活中,警方审讯靠的不仅仅是自信和创造力(尽管这两点对审讯工作确有帮助)——审讯者还要在交际影响的心理战术方面接受过高水平训练.       让一个人认罪可不是件容易事,而警察有时能让无辜者承认 ...

  6. 安装LINUX X86-64的10201出现链接ins_ctx.mk错误-转自yingtingkun

    详细错误信息为: Error in invoking target ‘install’ of makefile ‘/opt/oracle/product/10.2/ctx/lib/ins_ctx.mk ...

  7. nyoj 79 导弹拦截

    点击打开链接 拦截导弹 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 某国为了防御敌国的导弹袭击,发展中一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发 ...

  8. 百度之星IP聚合(水题map&字符处理)

    虽然题目停水的,但是好像字符处理运用的还比较合适 Problem Description 当今世界,网络已经无处不在了,小度熊由于犯了错误,当上了度度公司的网络管理员,他手上有大量的 IP列表,小度熊 ...

  9. C# odbc

    一直下一步,注意需要 勾选你要连接的库名 odbc 命名空间 System.Data.Odbc

  10. PetaPoco.Core.ttinclude修改

    /// <summary> /// Adds the singular rule. /// </summary> /// <param name="rule&q ...