最大的网站源码资源下载站,
using system.drawing;
using system.drawing.imaging;
private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
//rndnum是一个自定义函数
string vnum=rndnum(4); //这里的数字4代表显示的是4位的验证字符串!
session["vnum"]=vnum;
validatecode(vnum);
}
//生成图像函数
private void validatecode(string vnum)
{
int gheight=(int)(vnum.length * 11.5);
//gheight为图片宽度,根据字符长度自动更改图片宽度
system.drawing.bitmap img = new system.drawing.bitmap(gheight,20);
graphics g = graphics.fromimage(img);
g.drawstring(vnum,new system.drawing.font("arial",10),new system.drawing.solidbrush(color.red),3,3);
//在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y)
system.io.memorystream ms=new system.io.memorystream();
img.save(ms,system.drawing.imaging.imageformat.png);
response.clearcontent(); //需要输出图象信息 要修改http头
response.contenttype="image/png";
response.binarywrite(ms.toarray());
g.dispose();
img.dispose();
response.end();
}
http://community.csdn.net/expert/topic/3342/3342222.xml?temp=.5347406
//生成随机数函数中从vchar数组中随机抽取
//字母区分大小写
public string rndnum(int vcodenum)
{
string vchar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,w,x,y,z" ;
string[] vcarray = vchar.split(',') ;
string vnum = "" ;//由于字符串很短,就不用stringbuilder了
int temp = -1 ;//记录上次随机数值,尽量避免生产几个一样的随机数
//采用一个简单的算法以保证生成随机数的不同
random rand =new random();
for ( int i = 1 ; i < vcodenum+1 ; i++ )
{
if ( temp != -1)
{
rand =new random(i*temp*unchecked((int)datetime.now.ticks));
}
//int t = rand.next(35) ;
int t=rand.next(35);
if (temp != -1 && temp == t)
{
return rndnum( vcodenum );
}
temp = t ;
vnum += vcarray[t];
}
return vnum ;
}
-----------------------------------end--------------------------------------------
下面是login.aspx在在调用提交按钮的响应事件中
public void doit(object sender, system.eventargs e)
{
if(page.isvalid)
{
string vnum;
vnum=session["vnum"].tostring();
session.abandon();
viewstate["vnum"]=vnum;
if(this.vcode.text==viewstate["vnum"].tostring())
{
hover.manage.checklogin obj=new hover.manage.checklogin();
string name=username.text;
string password=formsauthentication.hashpasswordforstoringinconfigfile(pass.text.tostring(),"md5");
if(!obj.checklogin(name,password))
{
response.redirect("../error.aspx?action=errorlogin");
response.end();
return;
}
else
{
session.add("adminname",name);
session.add("adminpass",password);
response.redirect("default.aspx");
response.end();
return;
}
}
else
{
response.write("<script>alert(/"请输入正确的附加码!/");</script>");
}
}
}
如果提交过来的验证字符串正确就验证是否是合法用户!
否则弹出提示返回登陆页!你也可以简化此页!
if(this.vcode.text==viewstate["vnum"].tostring())
{
response.write("验证码正确");
}
else
{
response.write("验证码错误!");
}
新闻热点
疑难解答
图片精选