当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net生成验证码代码(纯中文)

asp.net生成验证码代码(纯中文)

2017年12月12日  | 移动技术网IT编程  | 我要评论

龙的天空,野兔影视,转角遇到爱演员表

复制代码 代码如下:

using system;
using system.data;
using system.configuration;
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.text; //添加引用
using system.drawing; //添加引用
/// <summary>
/// checkcode_ch 的摘要说明
/// </summary>
public class checkcode_ch
{
public checkcode_ch()
{
//
// todo: 在此处添加构造函数逻辑
//
}
private static object[] createstring()
{
//定义一个数组存储汉字编码的组成元素
string[] str = new string[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
random ran = new random(); //定义一个随机数对象
object[] bytes = new object[4];
for (int i = 0; i < 4; i++)
{
//获取区位码第一位
int ran1 = ran.next(11, 14);
string str1 = str[ran1].trim();
//获取区位码第二位并防止数据重复
ran = new random(ran1 * unchecked((int)datetime.now.ticks) + i);
int ran2;
if (ran1 == 13)
{
ran2 = ran.next(0, 7);
}
else
{
ran2 = ran.next(0, 16);
}
string str2 = str[ran2].trim();
//获取区位码第三位
ran = new random(ran2 * unchecked((int)datetime.now.ticks) + i);
int ran3 = ran.next(10, 16);
string str3 = str[ran3].trim();
//获取区位码第四位
ran = new random(ran3 * unchecked((int)datetime.now.ticks) + i);
int ran4;
if (ran3 == 10)
{
ran4 = ran.next(1, 16);
}
else if (ran3 == 15)
{
ran4 = ran.next(0, 15);
}
else
{
ran4 = ran.next(0, 16);
}
string str4 = str[ran4].trim();
//定义字节变量存储产生的随机汉字区位码
byte byte1 = convert.tobyte(str1 + str2, 16);
byte byte2 = convert.tobyte(str3 + str4, 16);
byte[] stradd = new byte[] { byte1, byte2 };
//将产生的汉字字节放入数组
bytes.setvalue(stradd, i);
}
return bytes;
}
private static string getstring()
{
encoding gb = encoding.getencoding("gb2312");
object[] bytes = createstring();
//根据汉字字节解码出中文汉字
string str1 = gb.getstring((byte[])convert.changetype(bytes[0], typeof(byte[])));
string str2 = gb.getstring((byte[])convert.changetype(bytes[1], typeof(byte[])));
string str3 = gb.getstring((byte[])convert.changetype(bytes[2], typeof(byte[])));
string str4 = gb.getstring((byte[])convert.changetype(bytes[3], typeof(byte[])));
string str = str1 + str2 + str3 + str4;
httpcontext.current.response.cookies.add(new httpcookie("checkcode", str));
return str;
}
public static void graphicsimage()
{
system.drawing.bitmap image = new system.drawing.bitmap((int)math.ceiling((getstring().length * 22.5)), 22);
graphics g = graphics.fromimage(image); //创建画布
try
{
//生成随机生成器
random random = new random();
//清空图片背景色
g.clear(color.white);
//画图片的背景噪音线
for (int i = 0; i < 1; i++)
{
int x1 = random.next(image.width);
int x2 = random.next(image.width);
int y1 = random.next(image.height);
int y2 = random.next(image.height);
g.drawline(new pen(color.black), x1, y1, x2, y2);
}
font font = new system.drawing.font("couriew new", 12, system.drawing.fontstyle.bold);
system.drawing.drawing2d.lineargradientbrush brush = new system.drawing.drawing2d.lineargradientbrush
(new rectangle(0, 0, image.width, image.height), color.blue, color.darkred, 1.2f, true);
g.drawstring(getstring(), font, brush, 2, 2);
//画图片的前景噪音点
for (int i = 0; i < 50; i++)
{
int x = random.next(image.width);
int y = random.next(image.height);
image.setpixel(x, y, color.fromargb(random.next()));
}
//画图片的边框线
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);
httpcontext.current.response.clearcontent();
httpcontext.current.response.contenttype = "image/gif";
httpcontext.current.response.binarywrite(ms.toarray());
}
catch (exception ms)
{
httpcontext.current.response.write(ms.message);
}
}
}

第二步建立一个页面引用类库chinesecheckcode.aspx前台不用写代码,后台引用类库。。
复制代码 代码如下:

using system;
using system.collections;
using system.configuration;
using system.data;
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;
public partial class uservalidator_chinesecheckcode : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
checkcode_ch.graphicsimage(); //调用方法生成四位汉字验证码
}
}

第三步引用验证码页
复制代码 代码如下:

<asp:textbox id="validator" runat="server" width="150px" ></asp:textbox>
<img id="img1" alt="看不清,请点击我!" onclick="this.src=this.src+'?'" src="chinesecheckcode.aspx"
style="width: 75px; height: 24px" align="left" />
<asp:imagebutton id="imgbtnlogin" runat="server" imageurl="~/images/login.gif"
onclick="imgbtnlogin_click" />

后台判断
复制代码 代码如下:

protected void imgbtnlogin_click(object sender, imageclickeventargs e)
{
httpcookie cookie = request.cookies["checkcode"];
if (cookie.value == this.validator.text.trim())
{
//。。。
}
else
{
response.write("<script>alert('验证码输入错误,请重新输入!');location='chinesecodevalidator.aspx'</script>");
return;
}
}

以上验证码生成四位,请各位根据 情况做适当修改。
现在总结了生成纯数字、数字字母混合、纯汉字的验证码技术。希望对各位有所帮助。。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网