您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

asp.net 验证码的简单制作(vb.net+C#)

2024/3/18 20:07:46发布10次查看
网站上验证码效果一般制作方法是:
1)使用httphandler(一般处理程序)绘制随机验证码的图,以及产生随机码,并输出到页面的outputstream中。
2)页面中使用异步方式(js等)进行刷新当前页面的验证码。
【示例】
1)创建一个“一般应用处理程序ashx”,代码如下:
[c#]
public class validationcode : ihttphandler { //随机发生器 static random r = new random(guid.newguid().gethashcode()); //排除黑色、透明色颜色,因为底色黑色 static propertyinfo[] colors = (typeof(brushes).getproperties(system.reflection.bindingflags.public | system.reflection.bindingflags.getproperty | system.reflection.bindingflags.static)).where(p => p.name != "black" && p.name != "transparent").select(p => p).toarray(); //排除黑色颜色,因为底色黑色 static propertyinfo[] linecolors = (typeof(pens).getproperties(system.reflection.bindingflags.public | system.reflection.bindingflags.getproperty | system.reflection.bindingflags.static)).where(p => p.name != "black").select(p => p).toarray(); //获取静态类brushes实例对象 static object colorobj = typeof(brushes).getconstructor(bindingflags.nonpublic, null, type.emptytypes, null); //获取静态类pens实例对象 static object penobj = typeof(pens).getconstructor(bindingflags.nonpublic, null, type.emptytypes, null); //每个随机字符的宽度 const float pernumberwidth = 40.0f; //每个字符的高度 const float pernumberheight = 50.0f; public void processrequest(httpcontext context) { //获取要产生多少随机数(默认产生5个) int reqnum = 5; if (context.request.querystring["reqnum"] != null) { int.tryparse(context.request.querystring["reqnum"], out reqnum); } //产生多少大的背景图 bitmap bt = new bitmap((int)(pernumberwidth*reqnum), (int)pernumberheight); graphics g = graphics.fromimage(bt); //产生4个随机数(number可以被保存到session中) string numbers = ""; //绘制数字 for (int i = 1; i <= reqnum; i++) { numbers += r.next(0, 9).tostring(); var color = (propertyinfo)colors.getvalue(r.next(0, colors.length)); context.response.write(color.name + "<br/>"); brush randomcolor = (brush)color.getvalue(colorobj, null); g.drawstring(numbers[i-1].tostring(), new font("黑体", pernumberwidth),randomcolor, new pointf((i-1)*pernumberwidth, 0f)); } //绘制随机线条 int linenum = r.next(10, 21); for (int i = 1; i <= linenum; i++) { var linecolor = (propertyinfo)linecolors.getvalue(r.next(0, colors.length)); pen randomcolor = (pen)linecolor.getvalue(penobj, null); g.drawline(randomcolor, new pointf((float)(r.nextdouble() * pernumberwidth * reqnum), (float)(r.nextdouble() * pernumberheight)), new pointf((float)(r.nextdouble() * pernumberwidth * reqnum), (float)(r.nextdouble() * pernumberheight))); } g.dispose(); context.response.clear(); context.response.contenttype = "image/jpeg"; bt.save(context.response.outputstream, imageformat.jpeg); bt.dispose(); context.response.end(); } public bool isreusable { get { return false; } } }
[vb.net]
public class validationcode implements ihttphandler '随机发生器 shared r as new random(guid.newguid().gethashcode()) '排除黑色、透明色颜色,因为底色黑色 shared colors as propertyinfo() = (gettype(brushes).getproperties(system.reflection.bindingflags.[public] or system.reflection.bindingflags.getproperty or system.reflection.bindingflags.[static])).where(function(p) p.name <> "black" andalso p.name <> "transparent").[select](function(p) p).toarray() '排除黑色颜色,因为底色黑色 shared linecolors as propertyinfo() = (gettype(pens).getproperties(system.reflection.bindingflags.[public] or system.reflection.bindingflags.getproperty or system.reflection.bindingflags.[static])).where(function(p) p.name <> "black").[select](function(p) p).toarray() '获取静态类brushes实例对象 shared colorobj as object = gettype(brushes).getconstructor(bindingflags.nonpublic, nothing, type.emptytypes, nothing) '获取静态类pens实例对象 shared penobj as object = gettype(pens).getconstructor(bindingflags.nonpublic, nothing, type.emptytypes, nothing) '每个随机字符的宽度 const pernumberwidth as single = 40f '每个字符的高度 const pernumberheight as single = 50f public sub processrequest(context as httpcontext) '获取要产生多少随机数(默认产生5个) dim reqnum as integer = 5 if context.request.querystring("reqnum") isnot nothing then integer.tryparse(context.request.querystring("reqnum"), reqnum) end if '产生多少大的背景图 dim bt as new bitmap(cint(math.truncate(pernumberwidth * reqnum)), cint(math.truncate(pernumberheight))) dim g as graphics = graphics.fromimage(bt) '产生4个随机数(number可以被保存到session中) dim numbers as string = "" '绘制数字 for i as integer = 1 to reqnum numbers += r.[next](0, 9).tostring() dim color = directcast(colors.getvalue(r.[next](0, colors.length)), propertyinfo) context.response.write(convert.tostring(color.name) & "<br/>") dim randomcolor as brush = directcast(color.getvalue(colorobj, nothing), brush) g.drawstring(numbers(i - 1).tostring(), new font("黑体", pernumberwidth), randomcolor, new pointf((i - 1) * pernumberwidth, 0f)) next '绘制随机线条 dim linenum as integer = r.[next](10, 21) for i as integer = 1 to linenum dim linecolor = directcast(linecolors.getvalue(r.[next](0, colors.length)), propertyinfo) dim randomcolor as pen = directcast(linecolor.getvalue(penobj, nothing), pen) g.drawline(randomcolor, new pointf(csng(r.nextdouble() * pernumberwidth * reqnum), csng(r.nextdouble() * pernumberheight)), new pointf(csng(r.nextdouble() * pernumberwidth * reqnum), csng(r.nextdouble() * pernumberheight))) next g.dispose() context.response.clear() context.response.contenttype = "image/jpeg" bt.save(context.response.outputstream, imageformat.jpeg) bt.dispose() context.response.[end]() end sub public readonly property isreusable() as boolean get return false end get end property end class
注意:
1)一些诸如brushes等特定因为是公用的,需要通过反射获取全部的颜色属性列表,因此使用了静态变量,这样不必每次都初始化,节省内存和时间。
2)brushes避免黑色和透明色(本示例背景色是黑色),pens只需避免黑色即可。有关brushes颜色,可以查阅:http://msdn.microsoft.com/zh-cn/library/system.windows.media.brush(v=vs.95).aspx
3)bitmap类是用于绘制使用的,一般是空白的黑色背景。一般配合image类+graphics画布使用,进行绘制。
4)bitmap的save方法有若干个重载版本,其中之一可以指定输出流以及设置图片格式。本示例就是使用了这个函数。
【应用】
html中代码(验证码部分,局部):
<h1> 验证码 </h1> <script> function changesd() { document.getelementbyid("imgsd").src = ""; document.getelementbyid("imgsd").src = "/validationcode.ashx?reqnum=10"; }; </script> <img src="/validationcode.ashx?reqnum=10" id="imgsd" /> <input type="button" value="change validation key" onclick="changesd()" />
注意,之所以使用js设置img的src两次,是因为重复的路径不会引发请求。
更多asp.net 验证码的简单制作(vb.net+c#)。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product