asp.net登录验证码实现方法
程序员文章站
2023-10-23 22:59:58
前端添加的标签和方法:
验证码:复制代码 代码如下:
前端添加的标签和方法:
验证码:
复制代码 代码如下:
<input id="txtverifycode" type="text" maxlength="5" style="line-height: 30px; height: 30px; width: 80px;border:solid 1px #d4d4d4;" class="input"/> <img src="" alt="点击刷新" id="imgvalidatecode" style="width: 100px; height: 30px; line-height: 30px; vertical-align: middle;" />  点击图片刷新</p>//标签
$(function () { $("#imgvalidatecode").click(function () { dofresh(); }); dofresh(); }) function dofresh() { var img = $("#imgvalidatecode"); img.attr("src", "verifycode.aspx?random=" + math.random()); } //添加的方法,src是生成图片的aspx的地址
然后在项目中在新建一个verifycode.aspx,下面是aspx的代码:
<%@ page language="c#" autoeventwireup="true" codebehind="verifycode.aspx.cs" inherits="form.verifycode" %> <!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 id="head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> </div> </form> </body> </html>
接着是aspx.cs的代码:
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.drawing; namespace form { public partial class verifycode : system.web.ui.page { public static string hz; /// <summary> /// 验证码的最大长度 /// </summary> public int maxlength { get { return 10; } } /// <summary> /// 验证码的最小长度 /// </summary> public int minlength { get { return 1; } } protected void page_load(object sender, eventargs e) { string[] str = createvalidatenumber(4); string strcode = string.empty; for (int i = 0; i < str.length; i++) { strcode += str[i]; } createcheckcodeimage(str); hz = strcode; response.write(hz); //验证码存入session session["validatecode"] = hz; } /// <summary> /// 生成验证码 /// </summary> /// <param name="length">指定验证码的长度</param> /// <returns>验证码</returns> public string[] createvalidatenumber(int length) { string vchar = "1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,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,p,q" + ",r,s,t,u,v,w,x,y,z"; string[] vcarray = vchar.split(new char[] { ',' });//拆分成数组 string[] num = new string[length]; int temp = -1;//记录上次随机数值,尽量避避免生产几个一样的随机数 random rand = new random(); //采用一个简单的算法以保证生成随机数的不同 for (int i = 1; i < length + 1; i++) { if (temp != -1) { rand = new random(i * temp * unchecked((int)datetime.now.ticks)); } int t = rand.next(vcarray.length-1); if (temp != -1 && temp == t) { return createvalidatenumber(length); } temp = t; num[i - 1] = vcarray[t]; //num.setvalue(vcarray[t]); //vnum += vcarray[t]; } return num; } private void createcheckcodeimage(string[] checkcode) { if (checkcode == null || checkcode.length <= 0) return; system.drawing.bitmap image = new system.drawing.bitmap((int)math.ceiling((checkcode.length * 32.5)), 60); system.drawing.graphics g = graphics.fromimage(image); try { //生成随机生成器 random random = new random(); //清空图片背景色 g.clear(color.white); //定义颜色 color[] c = { color.black, color.red, color.darkblue, color.green, color.orange, color.brown, color.darkcyan, color.purple }; //画图片的背景噪音线 for (int i = 0; i < 25; i++) { int cindex = random.next(7); int findex = random.next(5); 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(c[cindex]), x1, y1, x2, y2); } //定义字体 string[] f = { "verdana", "microsoft sans serif", "comic sans ms", "arial", "宋体" }; for (int k = 0; k <= checkcode.length - 1; k++) { int cindex = random.next(7); int findex = random.next(5); font drawfont = new font(f[findex], 26, (system.drawing.fontstyle.bold)); solidbrush drawbrush = new solidbrush(c[cindex]); float x = 5.0f; float y = 0.0f; float width = 42.0f; float height = 48.0f; int sjx = random.next(10); int sjy = random.next(image.height - (int)height); rectanglef drawrect = new rectanglef(x + sjx + (k * 25), y + sjy, width, height); stringformat drawformat = new stringformat(); drawformat.alignment = stringalignment.center; g.drawstring(checkcode[k], drawfont, drawbrush, drawrect, drawformat); } //画图片的前景噪音点 for (int i = 0; i < 500; i++) { int x = random.next(image.width); int y = random.next(image.height); image.setpixel(x, y, color.fromargb(random.next())); } int cindex1 = random.next(7); //画图片的边框线 g.drawrectangle(new pen(c[cindex1]), 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()); } finally { g.dispose(); image.dispose(); } } } }
于是!就可以生成验证码了,然后自己再把编写验证版的判断逻辑写好就可以啦!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。