Web应用验证码方面总结(ASP.NET版)

 

目录

Web应用验证码方面总结(ASP.NET版)    1

目录    2

修订历史纪录    3

1 验证码在Web应用上的作用    4

2 验证码示例    5

2.1    5

2.2 mail.qq.com    5

2.3 www.baidu.com    5

2.4 mail.gmail.cn    6

2.5 回答问题的验证码    6

3 验证码的识别技术    6

3.1 对比库    6

3.2 程序计算    6

3 验证码在ASP.NET下的实现    6

 

1 验证码在Web应用上的作用

    随着互联网的发展,上网冲浪的人越来越多。在巨大的商机面前,为互联网提供增值服务的机构与日剧增。在这些服务之中,有一大部分需要用户首先注册、登录站点之后,才可以享受它提供的服务。现在存在这样一种情况,别有用心的人使用编写的程序,大量的重复注册某一个站点(免费的),这使这个站点产生了大量无用的用户占用了服务器的存储资源。类似的情况还有很多,他们的共同点——就是使用自动的程序(比人手动快许多)向站点提供巨量的对这个站点毫无价值、甚至于有害的信息,影响站点正常的经营活动或是谋取私利。

    防止这种现象,是验证码出现的主要原因。他在页面上显示出一串信息,并要求用户作出正确的回应,如果做出的回应正确,用户才能继续或完成希望的操作。验证码要完成的就是防止机器程序能够自动的识别验证码的内容,同时要保证用户能够识别出来。

2 验证码示例

2.1

2.2 mail.qq.com

2.3 www.baidu.com

2.4 mail.gmail.cn

2.5 回答问题的验证码

    验证码是一个问题,如1+1,要求用户回答问题。

3 验证码的识别技术

    识别技术指,同样使用程序,自动的识别验证码的内容。

3.1 对比库

    做法就是建立一个数据库,存储所有字符的特征资料。在识别时读取页面上的图像,之后同特征库进行对比,从而识别验证码。

3.2 程序计算

    读取验证码的图像,搜集其中的像素信息,之后使用复杂的算法,排除干扰信息,计算出字符的内容。

    

3 验证码在ASP.NET下的实现

    使用.NET Framework的GDI+,绘制一个图像是比较常用的方法。

    主要涉及的内容:1 随机。产生的验证码内容、产生的验证码的颜色、三验证码在图像中出现的位置、干扰因素出现的位置,全部随机。2 干扰因素。为防止验证码被不明程序自动识别,要在图像中加入干扰因素阻扰程序的识别。

下面是一个较简单的例子,供参考(使用GDI+):

    这是一个ASP.NET页面的隐藏代码,使用它只需在另一个页面中设置一个Image控件,将他的src属性设置为这个页面即可。

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Drawing;

 

public partial class GenerateValidPicture : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

this.CreateCheckCodeImage(GenerateCheckCode());

}

private string GenerateCheckCode()

{

int number;

char code;

string checkCode = String.Empty;

System.Random random = new Random();

//generate five length string constitute of digit and letter.

for (int i = 0; i < 5; i++)

{

number = random.Next(); //generate a radom integer.

if (number % 2 == 0)

code = (char)('0' + (char)(number % 10)); //generate a digit char.

else

code = (char)('A' + (char)(number % 26)); //generate a letter char.

checkCode += code.ToString();

}

Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));

return checkCode;

}

private void CreateCheckCodeImage(string checkCode)

{

if (checkCode == null || checkCode.Trim() == String.Empty)

return;

System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);

Graphics g = Graphics.FromImage(image);

try

{

//生成随机生成器

Random random = new Random();

 

//清空图片背景色

g.Clear(Color.White);

 

//画图片的背景噪音线

for (int i = 0; i < 25; 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);

}

 

Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));

System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);

g.DrawString(checkCode, font, brush, 2, 2);

 

//画图片的前景噪音点

for (int i = 0; i < 100; 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), 0, 0, image.Width - 1, image.Height - 1);

 

System.IO.MemoryStream ms = new System.IO.MemoryStream();

image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);

Response.ClearContent();

Response.ContentType = "image/Gif";

Response.BinaryWrite(ms.ToArray());

}

finally

{

g.Dispose();

image.Dispose();

}

}

}

 

posted on 2007-11-29 18:55  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2007/11/29/977319.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值