日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET 验证码示例

發布時間:2025/6/15 asp.net 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET 验证码示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

<%@?Page?Language="C#"?%>
<%@?Import?Namespace="System.Drawing"?%>
<!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"?"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">?
<!--
????
1.準備Bitmap對象?image:
????????System.Drawing.Bitmap?image?
=?new?System.Drawing.Bitmap(iwidth,?20);
????
2.利用Graphics對象g在上面畫圖:
????????Graphics?g?
=?Graphics.FromImage(image);?
????????g.DrawString(checkCode,?f,?b,?
3,?3);?
????
3.然后Response.BinaryWrite輸出:
????????Response.BinaryWrite(ms.ToArray());
????優化想法:
????將驗證碼以js腳本可讀取的方式發送到客戶端;
????客戶端先出發js腳本的驗證,通過后才允許提交;
????提供重新獲取驗證碼功能。
-->
<script?runat="server">
????????
///?<summary>
????????
///?輸入要求的驗證碼的位數,返回一個驗證碼字符串
????????
///?</summary>
????????
///?<param?name="codeCount">驗證碼的位數</param>
????????
///?<returns>驗證碼字符串</returns>
????????private?string?CreateRandomCode(int?codeCount)
????????{
????????????
string?allChar?=?"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[]?allCharArray?=?allChar.Split(',');
????????????
int?allCharLen?=?allCharArray.Length;?
????????????
string?randomCode?=?"";?

????????????Random?rand?
=?new?Random();
????????????
for?(int?i?=?0;?i?<?codeCount;?i++)
????????????{
????????????????randomCode?
+=?allCharArray[rand.Next(0,?allCharLen)];
????????????}
????????????
return?randomCode;
????????}
????
????????
///?<summary>
????????
///?根據驗證碼checkCode畫圖
????????
///?</summary>
????????
///?<param?name="checkCode"></param>
????????private?void?CreateImage(string?checkCode)
????????{
????????????
int?iwidth?=?(int)(checkCode.Length?*?11.5);
????????????System.Drawing.Bitmap?image?
=?new?System.Drawing.Bitmap(iwidth,?20);
????????????Graphics?g?
=?Graphics.FromImage(image);
????????????Font?f?
=?new?System.Drawing.Font("Arial",?10,?System.Drawing.FontStyle.Bold);
????????????Brush?b?
=?new?System.Drawing.SolidBrush(Color.White);
????????????
//g.FillRectangle(new?System.Drawing.SolidBrush(Color.Blue),0,0,image.Width,?image.Height);
????????????g.Clear(Color.Blue);
????????????g.DrawString(checkCode,?f,?b,?
3,?3);?
????????????g.Dispose();

????????????
//Pen?blackPen?=?new?Pen(Color.Black,?0);
????????????
//Random?rand?=?new?Random();
????????????
//for?(int?i?=?0;?i?<?5;?i++)
????????????
//{
????????????
//????int?y?=?rand.Next(image.Height);
????????????
//????g.DrawLine(blackPen,?0,?y,?image.Width,?y);
????????????
//}?

????????????System.IO.MemoryStream?ms?
=?new?System.IO.MemoryStream();
????????????image.Save(ms,?System.Drawing.Imaging.ImageFormat.Jpeg);
????????????Response.ClearContent();
????????????Response.ContentType?
=?"image/Jpeg";
????????????Response.BinaryWrite(ms.ToArray());
????????????image.Dispose();
????????????
//---------緩存驗證碼,以備驗證---------
????????????Session["ValidateCode"]?=?checkCode;
????????????
/*如果希望在掛接該aspx頁的頁面A.aspx內執行Session的綁定就是白扯!
?????????????*?A頁面首先
綁定,然后是確定表達式,此時B.aspx未執行,還沒有產生Session呢!
?????????????*?
*/
????????}?????
????????
protected?void?Page_Load(object?sender,?EventArgs?e)
????????{
????????????CreateImage(CreateRandomCode(
6));
????????}
</script>?

<html?xmlns="http://www.w3.org/1999/xhtml"?>
<head?id="Head1"?runat="server">
????
<title>無標題頁</title>
</head>
<body>
</body>
</html>

調用上面的驗證程序:
<%@?Page?Language="C#"?%>?

<!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"?"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">?

<script?runat="server">?
????
protected?void?btnSend_Click(object?sender,?EventArgs?e)
????{
????????
if(Session["ValidateCode"].ToString()!=txtValidateCode.Text)
????????Response.Write(
"驗證碼不正確");
????????
else
????????Response.Write(
"登錄到新的界面!");
????}
</script>?

<html?xmlns="http://www.w3.org/1999/xhtml"?>
<head?id="Head1"?runat="server">
????
<title>無標題頁</title>

</head>
<body>
????
<form?id="form1"?runat="server">
????????
<div>
????????
<asp:TextBox?ID="txtValidateCode"?runat="server"></asp:TextBox>
????????
????????
<img?src="ValidateCode.aspx"?alt="驗證碼"?/>
????????
<br?/>
????????
<asp:Button?ID="btnSend"?runat="server"?OnClick="btnSend_Click"?Text="發送"?/></div>
????
</form>
</body>
</html>

轉載于:https://www.cnblogs.com/flaaash/archive/2007/12/14/995173.html

總結

以上是生活随笔為你收集整理的ASP.NET 验证码示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。