asp.net

位置:IT落伍者 >> asp.net >> 浏览文章

ASP.NET 做验证码


发布日期:2022年05月09日
 
ASP.NET 做验证码

显示验证码的界面

前台

<td class=style>

验证码</td>

<td class=style>

<asp:TextBox ID=TextBox runat=server Height=px></asp:TextBox>

<asp:Image ID=Image runat=server ImageUrl=~/Defaultaspx/>

&nbsp;<asp:HyperLink ID=HyperLink runat=server

NavigateUrl=~/Defaultaspx>看不清换张图</asp:HyperLink>

</td>

后台

using System;

using SystemCollectionsGeneric;

using SystemLinq;

using SystemWeb;

using SystemWebUI;

using SystemWebUIWebControls;

using SystemData;

using SystemDataSqlClient;

using SystemConfiguration;

public partial class _Default : SystemWebUIPage

{

protected void Page_Load(object sender EventArgs e)

{

}

protected void Button_Click(object sender EventArgs e)

{

string str = ConfigurationManagerConnectionStrings[sqlstr]ConnectionString;

using (SqlConnection sqlCnn=new SqlConnection(str))

{

using (SqlCommand sqlCmm=sqlCnnCreateCommand())

{

sqlCmmCommandText = select * from Login;

sqlCnnOpen();

SqlDataReader reader = sqlCmmExecuteReader();

bool bl = false;

if (reader!=null)

{

while (readerRead())

{

if (thisTextBoxText == reader[name]ToString() && thisTextBoxText == reader[password]ToString() && thisTextBoxText == Session[code]ToString())

{

bl = true;

}

}

if (bl==true)

{

ClientScriptRegisterClientScriptBlock(GetType() 提示 <script>alert(登陆成功!)</script>);

}

else

{

ClientScriptRegisterClientScriptBlock(GetType() 提示 <script>alert(登陆失败!)</script>);

}

}

}

}

}

}

做验证码的界面

using System;

using SystemCollectionsGeneric;

using SystemLinq;

using SystemWeb;

using SystemWebUI;

using SystemWebUIWebControls;

using SystemDrawing;

using SystemIO;

using SystemText;

public partial class Default : SystemWebUIPage

{

protected void Page_Load(object sender EventArgs e)

{

SystemDrawingImage img = new Bitmap();

Graphics g = GraphicsFromImage(img);

thisDealImage(img);

gDrawLine(PensYellow );

string code = thisGenerateCode();

Font font = new Font(宋体 FontStyleBold | FontStyleItalic | FontStyleStrikeout | FontStyleUnderline);

Session[code] = code; //保存到Session中

gDrawString(codefontBrushesYellowGreen);

thisResponseClear();

MemoryStream ms = new MemoryStream();

imgSave(msSystemDrawingImagingImageFormatJpeg);

thisResponseBinaryWrite(msToArray());

thisResponseFlush();

thisResponseEnd();

}

private void DealImage(SystemDrawingImage imgint nums)

{

Bitmap b = img as Bitmap;

Random ran = new Random();

for (int i = ; i < nums; i++)

{

bSetPixel(ranNext( imgWidth) ranNext(imgHeight)ColorWhite);

}

}

private string GenerateCode() //产生随机数

{

Random ran = new Random(DateTimeNowMillisecond);

StringBuilder sb = new StringBuilder();

for (int i = ; i <; i ++)

{

sbAppend(ranNext());

}

return sbToString();

}

}

               

上一篇:如何改变asp.net项目名称

下一篇:ASP.NET中处理异常的几种方式