网上搜的ASP.NET验证码,很实用

作者在 2012-09-28 12:34:13 发布以下内容
在ASP页面中生成图片验证码,需要用到System.Drawing命名空间下的很多类,首先我们需要新建一个CreateImage.aspx页面,在后台代码中定义用于生成验证码图片的方法,如下:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HTMLControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.XML.Linq;
using System.Drawing;

public partial class CreateImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//产生4位验证码
string CheckCode = CreateCode(4);
//用于验证
Session["code"] = CheckCode;
CreateImages(CheckCode);
}

/// <summary>
/// 产生验证码
/// </summary>
/// <param name="codeLength"></param>
/// <returns></returns>
public string CreateCode(int codeLength)
{
string so = "1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
string[] strArray = so.Split(',');
string code = "";
Random rand = new Random();
for (int i = 0; i < codeLength; i++)
{
//Random.Next(minValue,maxValue);
//一个大于或等于 minValue 且小于 maxValue 的 32 位带符号整数,即:返回的值范围包括 minValue 但不包括 maxValue。如果
//minValue 等于 maxValue,则返回 minValue。
code += strArray[rand.Next(0, strArray.Length)];
}
return code;
}

/// <summary>
/// 产生验证图片
/// </summary>
/// <param name="code"></param>
public void CreateImages(string code)
{
//创建一个Bitmap新实例
Bitmap image = new Bitmap(60, 20);
Graphics g = Graphics.FromImage(image);
WebColorConverter ww = new WebColorConverter();
//清楚整个绘图面,并以制定颜色填充
g.Clear((Color)ww.ConvertFromString("#FAE264"));

Random rand = new Random();
//画图片的背景噪音线
for (int i = 0; i < 12; i++)
{
int x1 = rand.Next(image.Width);
int x2 = rand.Next(image.Height);
int y1 = rand.Next(image.Width);
int y2 = rand.Next(image.Height);
g.DrawLine(new Pen(Color.LightGray), x1, y1, x2, y2);
}
//新建字体
Font font = new Font("Arial", 15, FontStyle.Bold | FontStyle.Italic);
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Gray, 1.2f, true);
g.DrawString(code, font, brush, 0, 0);

//画图片的前景噪音
for (int i = 0; i < 10; i++)
{
int x = rand.Next(image.Width);
int y = rand.Next(image.Height);
image.SetPixel(x, y, Color.White);
}

//画图片的边框线
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());

g.Dispose();
image.Dispose();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion

}

以上是CreateImage.aspx文件的后台代码,完成上述方法后,在页面里调用方法,只需要添加<img src="CreateImage.aspx" align="middle" />,即可显示生成的验证码图片,每次刷新都会随机产生不同的验证码,如下:

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

<!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>CheckImage</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<img src="CreateImage.aspx" align="middle" />
</div>
</form>
</body>
</html>


默认分类 | 阅读 1179 次
文章评论,共0条
游客请输入验证码