您的位置:首页 > 其它

Ajax实现验证码功能

2008-03-29 15:25 316 查看
 越来越多的网站为了实现一个防恶意刷屏,而产生了一种验证码的东东,这个玩意儿发送到前台其实是一个图片,而关键对比数据保存在Session,在下在网上看到很多都是用VB.net写的,而C#实现的确不多,在下使用AJAXPRO技术加上位图操作实现对图片的重绘。。。写了一个完整的网页验证码的东东,希望看得明白的人喜欢。。。

下面是源码:

  1。整个程序分为两个页面组成。

  第一个页面用来生成验证码的图像

  第二个页面是一个输入页面用来测试生成的验证码

下面是Validator.aspx内容,这个页面是为了生成一个随机数的图像,里面有非常详细的注释

/*下面是Validator.aspx.cs的代码*/ 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.Drawing2D;

using System.Drawing.Imaging;

using System.Drawing.Text;

using System.Drawing;

using System.Text;

using AjaxPro;

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

{

private readonly string strPicPath = "images\\Validator.jpg";

protected void Page_Load(object sender, EventArgs e)

{

string strOut = GetRandom(10000, 99999);

Session.Add("randomNum", strOut); //将随机数保存到Session

OutImages(strOut);

}

/// <summary>

/// 得到一个指定范围的随机数

/// </summary>

/// <param name="MinNumeric">随机数的最小选取范围</param>

/// <param name="MaxNumeric">随机数的最大选取范围</param>

/// <returns>随机数</returns>

private string GetRandom(int MinNumeric, int MaxNumeric)

{

long liResult = 0; //存放生成的随机数

Random ro = new Random(); //产生一个随机数实例

liResult = ro.Next(MinNumeric, MaxNumeric); //产生一个指定范围之间的随机数

return liResult.ToString(); //返回得到结果

}

/// <summary>

/// 将生成的随机数写入到生成的JPG文件中

/// </summary>

/// <param name="strOutput">生成的随机数</param>

private void OutImages(string strOutput)

{

Bitmap bitMaping = new Bitmap(Server.MapPath(strPicPath)); //创建一个位图文件得到它的句柄

Graphics graphicImage = Graphics.FromImage(bitMaping); //得到一个图形对象

graphicImage.SmoothingMode = SmoothingMode.HighSpeed;//设置此图形的画笔模式为高速

graphicImage.DrawString(strOutput, new Font("宋体", 16, FontStyle.Bold), SystemBrushes.WindowText, new Point(0, 0));

Response.ContentType = "image/jpeg"; //设置输出格式

bitMaping.Save(Response.OutputStream, ImageFormat.Jpeg);//保存数据流

graphicImage.Dispose();

bitMaping.Dispose(); //释放资源

}

}

下面是前端页面这里没什么讲的,就只一个页面而已

/*validator.aspx代码段*/ <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Validator.aspx.cs" Inherits="Validator" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>验证码生成页</title>

</head>

<body>

<form id="frmValidator" runat="server">

<div>

</div>

</form>

</body>

</html>

下面是Ajax的实现端,即前台的验证码实现代码段

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>无标题页</title>

<script language="javascript" type="text/javascript">

// <!CDATA[

function Button1_onclick() {

var strInput = document.getElementById("Text1").value;

var strMsg = "";

if( 1 == Test.CheckValidator(strInput).value)//调用ajax方法验证数据

{

strMsg = "验证码正确";

}

else

{

strMsg = "验证码错误";

}

document.getElementById("Label1").innerHTML = strMsg;

}

// ]]>

</script>

</head>

<body>

<form id="form1" runat="server">

<div>

<input id="Text1" type="text" />

<asp:Image ID="Image1" runat="server" ImageUrl="Validator.aspx" /><br />

<input id="Button1" type="button" value="验证" onclick="return Button1_onclick()" />

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>

</form>

</body>

</html>

这里是后台操作类

把前面存在Session中的代码与通过Ajax方法传进来的参数进行比较返回比较结果

再由Label标签打印出结果

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 AjaxPro;

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

{

protected void Page_Load(object sender, EventArgs e)

{

AjaxPro.Utility.RegisterTypeForAjax(typeof(Test));

}

[AjaxPro.AjaxMethod]

public int CheckValidator(string strInput)

{

int iState = 0;

if (strInput.Equals(Session["randomNum"].ToString()))

{

iState = 1;

}

return iState;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: