ASP.NET验证码实现(附源码)

  • 格式:pdf
  • 大小:92.34 KB
  • 文档页数:3

ASP.NET验证码实现(附源码)

⾸先看下效果实现(由于gif屏幕录制软件是即时找的,有些失祯)

代码主要就是绘制验证码类的实现

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Drawing;

using System.IO;

namespace SecurityCodePic

{

public class DrawingSecurityCode

{

///

/// ⽣成验证码,并返回

///

///

public string GetSecurityCode(int n)

{

string code = GenerateCheckCode(n);

CreateCheckCodeImage(code);

return code;

}

///

/// 动态⽣成指定数⽬的随机数或字母

///

/// 整数

/// 返回验证码字符串

private string GenerateCheckCode(int num)

{

int number;//定义变量

char code;

string checkCode = String.Empty; //空字符串,只读

Random random = new Random(); //定义随机变量实例

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

{

//利⽤for循环⽣成指定数⽬的随机数或字母

number = random.Next(); //返回⼀个⼩于指定的最⼤值的⾮负的随机数 next有三个构造函数

if (number % 2 == 0)

{//产⽣⼀个⼀位数

code = (char)('0' + (char)(number % 10));

}

else

{ //产⽣⼀个⼤写字母

code = (char)('A'+(char)(number % 26));

}

checkCode += code.ToString();

}

return checkCode;

}

///

/// 根据验证码字符串⽣成验证码图⽚

///

/// 验证码字符串

private void CreateCheckCodeImage(string checkCode)

{

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

// 引⽤System.Drawing类库

Bitmap myImage = new Bitmap(80, 30);//⽣成⼀个指定⼤⼩的位图

Graphics graphics = Graphics.FromImage(myImage); //从⼀个位图⽣成⼀个画布

try

{

graphics.Clear(Color.White); //清除整个绘画⾯并以指定的背景⾊填充,这⾥是把背景⾊设为⽩⾊

Random random = new Random(); //实例化⼀个伪随机数⽣成器

//画图⽚的前景噪⾳点,这⾥有100个 for (int i = 0; i < 100; i++)

{

int x = random.Next(myImage.Width);

int y = random.Next(myImage.Height);

myImage.SetPixel(x, y, Color.FromArgb(random.Next()));//指定坐标为x,y处的像素的颜⾊

}

//画图⽚的背景噪⾳线,这⾥为2条

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

{

int x1 = random.Next(myImage.Width);

int x2 = random.Next(myImage.Width);

int y1 = random.Next(myImage.Height);

int y2 = random.Next(myImage.Height);

graphics.DrawLine(new Pen(Color.Black), x1, y1, x2, y2); //绘制⼀条坐标x1,y1到坐标x2,y2的指定颜⾊的线条,这⾥的线条为⿊⾊

}

Font font = new Font("Arial", 15, FontStyle.Bold); //定义特定的⽂本格式,这⾥的字体为Arial,⼤⼩为15,字体加粗

//根据矩形、起始颜⾊和结束颜⾊以及⽅向⾓度产⽣⼀个LinearGradientBrush实例---线性渐变

System.Drawing.Drawing2D.LinearGradientBrush brush =

new System.Drawing.Drawing2D.LinearGradientBrush(

new Rectangle(0, 0, myImage.Width, myImage.Height),//在坐标0,0处实例化⼀个和myImage同样⼤⼩的矩形

Color.Blue, Color.Red, 1.2f, true);

//绘制⽂本字符串

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

//绘制有坐标对、宽度和⾼度指定的矩形---画图⽚的边框线

graphics.DrawRectangle(new Pen(Color.Silver), 0, 0, myImage.Width - 1, myImage.Height - 1);

//创建其⽀持存储器为内存的流

MemoryStream ms = new MemoryStream();

//将此图像以指定格式保存到指定的流中

myImage.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); //这⾥是以gif的格式保存到内存中

HttpContext.Current.Response.ClearContent(); //清除缓冲区流中的所有内容输出

HttpContext.Current.Response.ContentType = "image/Gif"; //获取或设置输出流的HTTP MIME类型

HttpContext.Current.Response.BinaryWrite(ms.ToArray()); //将⼀个⼆进制字符串写⼊HTTP输出流

}

finally

{

//释放占⽤资源

graphics.Dispose();

myImage.Dispose();

}

}

}

}

然后使⽤SecurityCode.ashx⽂件调⽤上⾯类的⽅法实现

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

namespace SecurityCodePic

{

///

/// SecurityCode1 的摘要说明

///

public class SecurityCode1 : IHttpHandler

{

public void ProcessRequest(HttpContext context)

{

DrawingSecurityCode sc = new DrawingSecurityCode();

string SecurityCode = sc.GetSecurityCode(6);

}

public bool IsReusable

{

get

{

return false;

}

}

}}

最后就是ASP.NET页⾯图⽚路径的引⽤了

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

验证码的实现

以上就是本⽂的全部,对了,还有源码下载分享给⼤家,欢迎⼤家下载。

源码分享: