ASP.NET MVC验证码功能实现代码
程序员文章站
2024-03-01 08:09:34
前台复制代码 代码如下:
<img id="vcodeimg" src="/home/vcode" width="70"
height="25" />
<span
style="cursor: pointer; text-decoration: underline">换一张</span>
控制器
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.mvc;
using utility;
using jellal**;
namespace sjlwebsite.controllers
{
public class commoncontroller : controller
{
#region 验证码
[outputcache(duration = 0)]
public actionresult vcode()
{
string code = validatecode.createrandomcode(4);
session["vcode"] = code;
validatecode.createimage(code);
return view();
}
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
namespace utility
{
/// <summary>
/// 完美随机验证码 0.10
/// verion:0.10
/// description:随机生成设定验证码,并随机旋转一定角度,字体颜色不同
/// </summary>
public class validatecode
{
/// <summary>
/// 生成随机码
/// </summary>
/// <param name="length">随机码个数www.52mvc.com</param>
/// <returns></returns>
public static string createrandomcode(int length)
{
int rand;
char code;
string randomcode = string.empty;
//生成一定长度的验证码
system.random random = new random();
for (int i = 0; i < length; i++)
{
rand = random.next();
if (rand % 3 == 0)
{
code = (char)('a' + (char)(rand % 26));
}
else
{
code = (char)('0' + (char)(rand % 10));
}
randomcode += code.tostring();
}
return randomcode;
}
/// <summary>
/// 创建随机码图片
/// </summary>
/// <param name="randomcode">随机码</param>
public static void createimage(string randomcode)
{
int randangle = 45; //随机转动角度
int mapwidth = (int)(randomcode.length * 23);
bitmap map = new bitmap(mapwidth, 28);//创建图片背景
graphics graph = graphics.fromimage(map);
graph.clear(color.aliceblue);//清除画面,填充背景
graph.drawrectangle(new pen(color.black, 0), 0, 0, map.width - 1, map.height - 1);//画一个边框
//graph.smoothingmode = system.drawing.drawing2d.smoothingmode.antialias;//模式
random rand = new random();
//背景噪点生成 www.jb51.net
pen blackpen = new pen(color.lightgray, 0);
for (int i = 0; i < 50; i++)
{
int x = rand.next(0, map.width);
int y = rand.next(0, map.height);
graph.drawrectangle(blackpen, x, y, 1, 1);
}
//验证码旋转,防止机器识别
char[] chars = randomcode.tochararray();//拆散字符串成单字符数组
//文字距中
stringformat format = new stringformat(stringformatflags.noclip);
format.alignment = stringalignment.center;
format.linealignment = stringalignment.center;
//定义颜色
color[] c = { color.black, color.red, color.darkblue, color.green, color.orange, color.brown, color.darkcyan, color.purple };
//定义字体
string[] font = { "verdana", "microsoft sans serif", "comic sans ms", "arial", "宋体" };
for (int i = 0; i < chars.length; i++)
{
int cindex = rand.next(7);
int findex = rand.next(5);
font f = new system.drawing.font(font[findex], 13, system.drawing.fontstyle.bold);//字体样式(参数2为字体大小)
brush b = new system.drawing.solidbrush(c[cindex]);
point dot = new point(16, 16);
//graph.drawstring(dot.x.tostring(),fontstyle,new solidbrush(color.black),10,150);//测试x坐标显示间距的
float angle = rand.next(-randangle, randangle);//转动的度数
graph.translatetransform(dot.x, dot.y);//移动光标到指定位置
graph.rotatetransform(angle);
graph.drawstring(chars.tostring(), f, b, 1, 1, format);
//graph.drawstring(chars.tostring(),fontstyle,new solidbrush(color.blue),1,1,format);
graph.rotatetransform(-angle);//转回去
graph.translatetransform(2, -dot.y);//移动光标到指定位置
}
//graph.drawstring(randomcode,fontstyle,new solidbrush(color.blue),2,2); //标准随机码
//生成图片
system.io.memorystream ms = new system.io.memorystream();
map.save(ms, system.drawing.imaging.imageformat.gif);
httpcontext.current.response.clearcontent();
httpcontext.current.response.contenttype = "image/gif";
httpcontext.current.response.binarywrite(ms.toarray());
graph.dispose();
map.dispose();
}
前台
复制代码 代码如下:
<img id="vcodeimg" src="/home/vcode" width="70"
height="25" />
<span
style="cursor: pointer; text-decoration: underline">换一张</span>
控制器
复制代码 代码如下:
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.mvc;
using utility;
using jellal**;
namespace sjlwebsite.controllers
{
public class commoncontroller : controller
{
#region 验证码
[outputcache(duration = 0)]
public actionresult vcode()
{
string code = validatecode.createrandomcode(4);
session["vcode"] = code;
validatecode.createimage(code);
return view();
}
public string getcode()
{
return session["vcode"].tostr();
}
#endregion
}
}
验证码类
复制代码 代码如下:
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
namespace utility
{
/// <summary>
/// 完美随机验证码 0.10
/// verion:0.10
/// description:随机生成设定验证码,并随机旋转一定角度,字体颜色不同
/// </summary>
public class validatecode
{
/// <summary>
/// 生成随机码
/// </summary>
/// <param name="length">随机码个数www.52mvc.com</param>
/// <returns></returns>
public static string createrandomcode(int length)
{
int rand;
char code;
string randomcode = string.empty;
//生成一定长度的验证码
system.random random = new random();
for (int i = 0; i < length; i++)
{
rand = random.next();
if (rand % 3 == 0)
{
code = (char)('a' + (char)(rand % 26));
}
else
{
code = (char)('0' + (char)(rand % 10));
}
randomcode += code.tostring();
}
return randomcode;
}
/// <summary>
/// 创建随机码图片
/// </summary>
/// <param name="randomcode">随机码</param>
public static void createimage(string randomcode)
{
int randangle = 45; //随机转动角度
int mapwidth = (int)(randomcode.length * 23);
bitmap map = new bitmap(mapwidth, 28);//创建图片背景
graphics graph = graphics.fromimage(map);
graph.clear(color.aliceblue);//清除画面,填充背景
graph.drawrectangle(new pen(color.black, 0), 0, 0, map.width - 1, map.height - 1);//画一个边框
//graph.smoothingmode = system.drawing.drawing2d.smoothingmode.antialias;//模式
random rand = new random();
//背景噪点生成 www.jb51.net
pen blackpen = new pen(color.lightgray, 0);
for (int i = 0; i < 50; i++)
{
int x = rand.next(0, map.width);
int y = rand.next(0, map.height);
graph.drawrectangle(blackpen, x, y, 1, 1);
}
//验证码旋转,防止机器识别
char[] chars = randomcode.tochararray();//拆散字符串成单字符数组
//文字距中
stringformat format = new stringformat(stringformatflags.noclip);
format.alignment = stringalignment.center;
format.linealignment = stringalignment.center;
//定义颜色
color[] c = { color.black, color.red, color.darkblue, color.green, color.orange, color.brown, color.darkcyan, color.purple };
//定义字体
string[] font = { "verdana", "microsoft sans serif", "comic sans ms", "arial", "宋体" };
for (int i = 0; i < chars.length; i++)
{
int cindex = rand.next(7);
int findex = rand.next(5);
font f = new system.drawing.font(font[findex], 13, system.drawing.fontstyle.bold);//字体样式(参数2为字体大小)
brush b = new system.drawing.solidbrush(c[cindex]);
point dot = new point(16, 16);
//graph.drawstring(dot.x.tostring(),fontstyle,new solidbrush(color.black),10,150);//测试x坐标显示间距的
float angle = rand.next(-randangle, randangle);//转动的度数
graph.translatetransform(dot.x, dot.y);//移动光标到指定位置
graph.rotatetransform(angle);
graph.drawstring(chars.tostring(), f, b, 1, 1, format);
//graph.drawstring(chars.tostring(),fontstyle,new solidbrush(color.blue),1,1,format);
graph.rotatetransform(-angle);//转回去
graph.translatetransform(2, -dot.y);//移动光标到指定位置
}
//graph.drawstring(randomcode,fontstyle,new solidbrush(color.blue),2,2); //标准随机码
//生成图片
system.io.memorystream ms = new system.io.memorystream();
map.save(ms, system.drawing.imaging.imageformat.gif);
httpcontext.current.response.clearcontent();
httpcontext.current.response.contenttype = "image/gif";
httpcontext.current.response.binarywrite(ms.toarray());
graph.dispose();
map.dispose();
}
}
}