您的位置:首页 > 编程语言 > ASP

ASP.NET中动态生成验证码的一则方法

2013-04-10 11:36 501 查看
现在不少网站中都使用了验证码的技术,实现方式也是多种多样,这里主要介绍ASP.NET中可以采用的一种动态生成验证码的方法,可能并不十分完美,但实现难度是属于较低的。

该方法是利用了普通的动态图片生成技术,但比较特别的一点是图片的生成是在一个Page类型的子类的Page_Load方法中执行的。所以Response的ContentType为image/Gif,而非text/html。

GraphicalText.aspx.cs代码:

using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace WebApplication1
{
public partial class GraphicalText : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
using (Bitmap image = new Bitmap(30, 20))
{
using (Graphics g = Graphics.FromImage(image))
{
g.FillRectangle(Brushes.Yellow, 0, 0, 30, 20);
g.DrawRectangle(Pens.Red, 0, 0, 29, 19);
Font f = new Font("Arial", 9, FontStyle.Italic);
string code = Request.QueryString["code"];
g.DrawString(code, f, Brushes.Blue, 0, 0);
Response.ContentType = "image/Gif";
image.Save(Response.OutputStream, ImageFormat.Gif);
}
}
}
}
}

注意,必须要加上这句代码——“Response.ContentType = “image/Gif”;”,否则在IE之外的浏览器中无法正确显示。

对应的GraphicalText.aspx代码很简单,只有一行,因为不需要有HTML的输出:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GraphicalText.aspx.cs" Inherits="WebApplication1.GraphicalText" %>

在主页面中关键要将图片的ImageUrl赋值为之前的页面地址,并且为了令图片内容发生变化,将4位随机数字作为它的参数。

Default.aspx.cs代码:

using System;
using System.Text;

namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder("~/GraphicalText.aspx?code=");
Random d = new Random();
sb.Append((char)d.Next(48, 58));
sb.Append((char)d.Next(48, 58));
sb.Append((char)d.Next(48, 58));
sb.Append((char)d.Next(48, 58));
Image1.ImageUrl = sb.ToString();
}
}
}


最后在页面中加上必要的验证代码,所有工作就都完成了。

Default.aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!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 type="text/javascript">
function ClientValidate(sender, args) {
var url = form1.Image1.src;
var index = url.lastIndexOf("=");
var code = url.substring(index + 1);
if (code == form1.TextBox1.value) {
args.IsValid = true;
}
else {
args.IsValid = false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1"  runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Image ID="Image1" runat="server"/>
<asp:Button ID="Button1" runat="server" Text="刷新"  />
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="验证码错误!"
ControlToValidate="TextBox1" ClientValidationFunction="ClientValidate" ValidateEmptyText="true" ValidationGroup="validation"></asp:CustomValidator>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<div>
<asp:Button ID="Button2" runat="server" Text="提交" ValidationGroup="validation"/>
</div>
</form>
</body>
</html>


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