国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

Asp.net 實現(xiàn)驗證碼功能的Web控件

2024-07-10 12:56:26
字體:
供稿:網(wǎng)友
asp.net的設(shè)計方式和設(shè)計理念和其他的如asp,jsp,php,perl

等都不一樣,幾乎是完全的面向?qū)ο笤O(shè)計!代碼的復(fù)用就是其

中差異較大的特點之一,asp.net除了可以用include以外,還提供

了比較有特點的web控件,包括:ascx形式和帶設(shè)計時支持的控

件[本文屬于后者],為了熟悉這些新概念,我自己寫了個web控件。

在實際項目中運行使用良好,以后,要有時間,我還將不斷改進(jìn)。


validatecode控件的使用方法:

第一步:

編譯我提供的原代碼, 然后,在studio.net 2003工具欄上, 選擇"添加/移除項", 選中編譯好的dll文件。

第二步:

工具欄上就會多一個web控件validatecode,做好一個web窗體,在studio.net 2003開發(fā)界面上,直接把控件拖到webform上,就ok!

第三步:

在該控件的graphicok事件中獲取,驗證碼的字符信息,用于和用戶錄入做比較!

最后一步:

在網(wǎng)站的根目錄下,建一個temp目錄(也可以自己指定目錄),用于存放驗證碼

圖片,不用擔(dān)心,代碼會自動刪除無用的圖片!


原代碼如下:

using system;
using system.web.ui;
using system.web.ui.webcontrols;
using system.componentmodel;
using system.io;
using system.drawing;
using system.drawing.drawing2d;
using system.drawing.imaging;

namespace webvalidatecode
{
/// <summary>
/// validatecode 的摘要說明。
/// 設(shè)計者:王海波 2004-11-20
/// </summary>
///
public enum graphictype
{
jpg = 0,
gif = 1,
png = 2,
bmp = 3,
}


//[toolboxbitmap(@"d:/dotnetapp/validatecode/validatecode.bmp") ] //設(shè)置控件在工具箱上的圖標(biāo)
public class validatecode : system.web.ui.webcontrols.webcontrol ,inamingcontainer
{
private int pcodelen=5;
private int pchartwidth=100;
private int pchartheight=20;

private graphictype pcharttype;

private string pauthencode;

private string ptempimageurlpath="/temp";
private string pauthenimagefullname;
private string pauthenimagefullurl;

//生成校驗碼的變量 start
private bitmap validateimage;
private graphics g;
//生成校驗碼的變量 end

private textbox txt=new textbox();
private system.web.ui.webcontrols.image img= new system.web.ui.webcontrols.image();

#region 定義控件事件

public delegate void graphiccreated(object sender, eventargs e);
public event eventhandler graphicok; //在校驗圖片生成結(jié)束以后觸發(fā)

protected virtual void ongraphicok(object sender, eventargs e)
{
if (graphicok != null)
{
//invokes the delegates.
graphicok(sender, e);
}
}

#endregion

#region 控件屬性

//生成校驗碼的長度
[bindable(true),browsable(true),category("appearance"),defaultvalue(true),description("需要驗證碼的長度,建議在5~8位之間!")]
public int codelength
{
get
{
return pcodelen;
}

set
{
pcodelen = value;
}
}

//生成校驗碼的長度
[bindable(true),browsable(true),category("appearance"),defaultvalue(true),description("生成驗證碼圖片的臨時存放路徑,要求必須是網(wǎng)站下的虛擬目錄!")]
public string tempimageurlpath
{
get
{
return ptempimageurlpath;
}

set
{
ptempimageurlpath = value;
}
}

[bindable(true),browsable(true),category("appearance"),defaultvalue(graphictype.jpg),description("選擇生成校驗圖文件的類型(jpg;gif;png;bmp)!")]
public graphictype charttype
{
get
{
return pcharttype;
}

set
{
pcharttype = value;
}
}


//生成校驗碼圖片的寬度
public int chartwidth
{
get
{
return pchartwidth;
}

set
{
pchartwidth = value;
}
}

//生成校驗碼圖片的高度
public int chartheight
{
get
{
return pchartheight;
}

set
{
pchartheight = value;
}
}

//需要生成的校驗碼
public string authencode
{
get
{
return pauthencode;
}

set
{
pauthencode = value;
}
}

#endregion

/// <summary>
/// 將此控件呈現(xiàn)給指定的輸出參數(shù)。
/// </summary>
/// <param name="output"> 要寫出到的 html 編寫器 </param>
protected override void render(htmltextwriter output)
{

system.web.ui.webcontrols.image objimage;
//textbox objtxt;


//繪制包含的控件
objimage = (system.web.ui.webcontrols.image) controls[0];
//objtxt = (textbox) controls[1];

if(pauthencode==null)
pauthencode=getvalidatecode();

ongraphicok(this,eventargs.empty );

getrandomimage(pauthencode);
objimage.imageurl=pauthenimagefullurl;

objimage.rendercontrol(output);

}

/// <summary>
/// 給控件添加子控件
/// </summary>
protected override void createchildcontrols( )
{

//controls.add(btn);
controls.add(img);
//controls.add(txt);
}

/// <summary>
/// 控件load時候?qū)傩缘某跏蓟?br> /// </summary>
/// <param name="e"></param>
protected override void onload(system.eventargs e)
{
eraseoldgraphic(); //刪除過期的圖片
}

/// <summary>
/// 生成隨機(jī)的
/// </summary>
private void makerandomfilename()
{
string strrandname=datetime.now.ticks.tostring()+".jpg";
pauthenimagefullname=this.page.mappath(tempimageurlpath)[email protected]"/"+strrandname;
pauthenimagefullurl=tempimageurlpath+"/"+strrandname;
}


private void getrandomimage(string strvalidatecode)
{
//生成隨即圖片的全名,和全url
makerandomfilename();

validateimage = new bitmap(pchartwidth, pchartheight, pixelformat.format24bpprgb); // .format24bpprgb);
g = graphics.fromimage(validateimage);

g.clear(color.lightgray) ;

//g.drawstring(strvalidatecode , new font("宋體",16,fontstyle.bold),new solidbrush(color.darkred),new pointf(2,2));

for(int i=0;i<strvalidatecode.length;i++)
{
random r = new random();
pointf startpos=new pointf(r.next(3,6)+(r.next(12,14)*i ),r.next(-1,2) );

g.drawstring(strvalidatecode.substring(i,1) , new font("宋體",14,fontstyle.italic),new solidbrush(color.blue),startpos);
}


//g.fillrectangle(new lineargradientbrush(new point(0,0), new point(120,30), color.fromargb(0,0,0,0),color.fromargb(255,255,255,255)),0,0,120,30);
switch(pcharttype)
{
case graphictype.jpg:

validateimage.save(pauthenimagefullname, imageformat.jpeg);
break;

case graphictype.gif:

validateimage.save(pauthenimagefullname, imageformat.gif);
break;

case graphictype.png:

validateimage.save(pauthenimagefullname, imageformat.png);
break;

case graphictype.bmp:

validateimage.save(pauthenimagefullname, imageformat.bmp);
break;

default:
validateimage.save(pauthenimagefullname, imageformat.jpeg);
break;

}

validateimage.dispose();

g.dispose();



}

/// <summary>
/// 動態(tài)從數(shù)字和字母組成的元素中動態(tài)選擇生成校驗碼
/// </summary>
private string getvalidatecode()
{
char[] s = new char[]{'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','v','w','x','y','z','a','b','c','d','e','f','g'
,'h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w'
,'x','y','z'};
string num = "";
random r = new random();

//根據(jù)用戶需要的長度來定義驗證碼的位數(shù)
for(int i = 0; i < codelength; i++)
{
num += s[r.next(0, s.length)].tostring();
}

return num;
}

/// <summary>
/// 清除時間超過20秒的臨時圖片記錄
/// </summary>
/// <returns>成功返回true,失敗返回false</returns>
public bool eraseoldgraphic()
{

try
{
directoryinfo dinfo=new directoryinfo(this.page.mappath(ptempimageurlpath));
fileinfo[] fileset;

if(dinfo.exists)
{
switch(pcharttype)
{
case graphictype.jpg:

fileset=dinfo.getfiles("*.jpg");
break;

case graphictype.gif:

fileset=dinfo.getfiles("*.gif");
break;

case graphictype.png:

fileset=dinfo.getfiles("*.png");
break;

case graphictype.bmp:

fileset=dinfo.getfiles("*.bmp");
break;

default:
fileset=dinfo.getfiles("*.jpg");
break;

}

foreach(fileinfo fileinfo in fileset)
{
if(fileinfo.exists)
{
datetime dts=datetime.now;
datetime dtc=fileinfo.creationtime;

timespan ts=dts-dtc;

if(ts.seconds>20)
{
fileinfo.delete();
}
}
}
}

return true;
}
catch(ioexception ioe)
{
return false;
}
}

}
}




發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 万年县| 安远县| 康乐县| 尖扎县| 澄迈县| 容城县| 贵州省| 佛山市| 丹阳市| 榆社县| 陇西县| 乌鲁木齐县| 穆棱市| 耒阳市| 高州市| 安陆市| 大庆市| 景洪市| 汝南县| 五峰| 玉龙| 鱼台县| 老河口市| 白银市| 如皋市| 瑞昌市| 托克逊县| 崇明县| 依兰县| 简阳市| 教育| 顺平县| 盐津县| 彭水| 桂平市| 宁晋县| 金川县| 晴隆县| 海兴县| 吴江市| 宝坻区|