asp.net驗證碼的簡單製作_實用技巧

來源:互聯網
上載者:User

實際上關於asp.net驗證碼製作的文章已經很多很多了,但是今天還是要和大家繼續分享,親,可以綜合幾篇執行個體,編寫出適用於自己網站的ASP.NET驗證碼,大概也就兩大部分:

先建立一個asp.net表單ValidateCode.aspx;不寫任何東西。直接在後台ValidateCode.aspx.cs中寫如下代碼:

    protected void Page_Load(object sender, EventArgs e)    {            string validateCode = CreateValidateCode();//產生驗證碼       Bitmap bitmap = new Bitmap(imgWidth,imgHeight);//產生Bitmap映像       DisturbBitmap(bitmap); //映像背景       DrewValidateCode(bitmap,validateCode);//繪製驗證碼映像       bitmap.Save(Response.OutputStream,ImageFormat.Gif);//儲存映像,等待輸出     }    private int codeLen = 4;//驗證碼長度     private int fineness = 85;//圖片清晰度     private int imgWidth = 48;//圖片寬度     private int imgHeight = 24;//圖片高度     private string fontFamily = "Times New Roman";//字型名稱     private int fontSize = 14;//字型大小     //private int fontStyle = 0;//字型樣式     private int posX = 0;//繪製起始座標X     private int posY = 0;//繪製座標Y     private string CreateValidateCode() //產生驗證碼     {      string validateCode = "";      Random random = new Random();// 隨機數對象       for (int i = 0; i < codeLen; i++)//迴圈產生每位元值       {        int n = random.Next(10);//數字         validateCode += n.ToString();      }      Session["vcode"] = validateCode;//儲存驗證碼 這Session是在前台調用的。      return validateCode;// 返回驗證碼     }    private void DisturbBitmap(Bitmap bitmap)//映像背景     {      Random random = new Random();//通過隨機數產生       for (int i = 0; i < bitmap.Width; i++)//通過迴圈嵌套,逐個像素點產生       {        for (int j = 0; j < bitmap.Height; j++)        {          if (random.Next(90) <= this.fineness)            bitmap.SetPixel(i, j, Color.LightGray);        }      }    }    private void DrewValidateCode(Bitmap bitmap, string validateCode)//繪製驗證碼映像     {      Graphics g = Graphics.FromImage(bitmap);//擷取繪製器對象       Font font = new Font(fontFamily, fontSize, FontStyle.Bold);//設定繪製字型       g.DrawString(validateCode, font, Brushes.Black, posX, posY);//繪製驗證碼映像     } 

在Login.aspx表單頁面中實現如下圖功能:

Login.aspx表單前台:

//這個函數是在點擊驗證碼圖片就會更換驗證碼//可以使用微軟內建的jqury.js 下面jquery-1.4.1.min.js版本之上的。或者在jquery官網上下載就可以。 <script src="styles/jquery-1.4.1.min.js" type="text/javascript"></script>     function f_refreshtype() {       var Image1 = document.getElementByIdx_x_x_x("img");       if (Image1 != null) {         Image1.src = Image1.src + "?";       }     }---<img src="ValidateCode.aspx" id="img" onclick="f_refreshtype()" width="50px"/>//調用函數,實現更換驗證碼

後台代碼:點擊登入驗證使用者是否輸入正確。 

  string usercode = txtcode.Text.Trim();      if (usercode == Session["vcode"].ToString())//Session["vcode"]      {  }

其他代碼就是跟其他一樣。

以上就是跟大家分享的關於產生ASP.NET驗證碼的過程,希望大家可以學以致用。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.