主頁(yè) > 知識(shí)庫(kù) > asp.net生成驗(yàn)證碼(純數(shù)字)

asp.net生成驗(yàn)證碼(純數(shù)字)

熱門標(biāo)簽:Mysql連接數(shù)設(shè)置 電子圍欄 阿里云 科大訊飛語(yǔ)音識(shí)別系統(tǒng) Linux服務(wù)器 銀行業(yè)務(wù) 團(tuán)購(gòu)網(wǎng)站 服務(wù)器配置
CheckCode.cs
復(fù)制代碼 代碼如下:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
/// summary>
/// CheckCode 的摘要說明
/// /summary>
public class CheckCode
{
public CheckCode()
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
public static void DrawImage()
{
CheckCode img = new CheckCode();
HttpContext.Current.Session["CheckCode"] = img.RndNum(4);
img.checkCodes(HttpContext.Current.Session["CheckCode"].ToString());
}
/// summary>
/// 生成驗(yàn)證圖片
/// /summary>
/// param name="checkCode">驗(yàn)證字符/param>
private void checkCodes(string checkCode)
{
int iwidth = (int)(checkCode.Length * 13);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 23);
Graphics g = Graphics.FromImage(image);
g.Clear(Color.White);
//定義顏色
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
//定義字體
string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋體" };
Random rand = new Random();
//隨機(jī)輸出噪點(diǎn)
for (int i = 0; i 50; i++)
{
int x = rand.Next(image.Width);
int y = rand.Next(image.Height);
g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);
}
//輸出不同字體和顏色的驗(yàn)證碼字符
for (int i = 0; i checkCode.Length; i++)
{
int cindex = rand.Next(7);
int findex = rand.Next(5);
Font f = new System.Drawing.Font(font[findex], 10, System.Drawing.FontStyle.Bold);
Brush b = new System.Drawing.SolidBrush(c[cindex]);
int ii = 4;
if ((i + 1) % 2 == 0)
{
ii = 2;
}
g.DrawString(checkCode.Substring(i, 1), f, b, 3 + (i * 12), ii);
}
//畫一個(gè)邊框
g.DrawRectangle(new Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 1);
//輸出到瀏覽器
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
HttpContext.Current.Response.ClearContent();
//Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/Jpeg";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
/// summary>
/// 生成隨機(jī)的字母
/// /summary>
/// param name="VcodeNum">生成字母的個(gè)數(shù)/param>
/// returns>string/returns>
private string RndNum(int VcodeNum)
{
string Vchar = "0,1,2,3,4,5,6,7,8,9";
string[] VcArray = Vchar.Split(',');
string VNum = ""; //由于字符串很短,就不用StringBuilder了
int temp = -1; //記錄上次隨機(jī)數(shù)值,盡量避免生產(chǎn)幾個(gè)一樣的隨機(jī)數(shù)
//采用一個(gè)簡(jiǎn)單的算法以保證生成隨機(jī)數(shù)的不同
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(VcArray.Length);
if (temp != -1 temp == t)
{
return RndNum(VcodeNum);
}
temp = t;
VNum += VcArray[t];
}
return VNum;
}
}

再建立一個(gè)引用類的頁(yè)面checkCode.aspx前臺(tái)不用寫東西,后臺(tái)引用我們創(chuàng)建的類的DrawImage()方法即可。
復(fù)制代碼 代碼如下:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class checkCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CheckCode.DrawImage();
}
}

下面我們?cè)谛枰?yàn)證碼的頁(yè)面引用checkCode.aspx頁(yè)面即可。
前臺(tái)
復(fù)制代碼 代碼如下:

asp:TextBox ID="Validator" runat="server" Width="150px" >/asp:TextBox>
img id="Img1" alt="看不清,請(qǐng)點(diǎn)擊我!" onclick="this.src=this.src+'?'" src="checkCode.aspx"
style="width: 73px; height: 22px" align="left" />
asp:ImageButton ID="imgBtnLogin" runat="server" ImageUrl="~/Images/Login.GIF"
OnClick="imgBtnLogin_Click" />

后臺(tái)判斷
復(fù)制代碼 代碼如下:

protected void imgBtnLogin_Click(object sender, ImageClickEventArgs e)
{
if(this.Validator.Text==Session["CheckCode"].ToString())
{
//。。。。
}
else
{
Response.Write("script>alert('驗(yàn)證碼輸入錯(cuò)誤,請(qǐng)重新輸入!');Location='MumberValidate.aspx'/script>");
return;
}
}

以上代碼請(qǐng)根據(jù)實(shí)際情況作適當(dāng)修改。
您可能感興趣的文章:
  • asp.net 驗(yàn)證碼生成和刷新及驗(yàn)證
  • asp.net(C#) 生成隨機(jī)驗(yàn)證碼的代碼
  • asp.net 簡(jiǎn)單驗(yàn)證碼驗(yàn)證實(shí)現(xiàn)代碼
  • ASP.NET MVC驗(yàn)證碼功能實(shí)現(xiàn)代碼
  • ASP.NET中的無刷新驗(yàn)證碼的開發(fā)(完整代碼)
  • asp.net 圖片驗(yàn)證碼的HtmlHelper
  • asp.net中3種驗(yàn)證碼示例(實(shí)現(xiàn)代碼)(數(shù)字,數(shù)字字母混和,漢字)
  • asp.net 驗(yàn)證碼的簡(jiǎn)單制作(vb.net+C#)
  • asp.net下生成英文字符數(shù)字驗(yàn)證碼的代碼
  • asp.net生成字母和數(shù)字混合圖形驗(yàn)證碼
  • Asp.net開發(fā)之webform圖片水印和圖片驗(yàn)證碼的實(shí)現(xiàn)方法
  • ASP.NET實(shí)現(xiàn)的生成驗(yàn)證碼功能示例【附demo源碼】

標(biāo)簽:萍鄉(xiāng) 衢州 蚌埠 棗莊 江蘇 廣元 大理 衡水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《asp.net生成驗(yàn)證碼(純數(shù)字)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266