asp.net登錄驗(yàn)證碼實(shí)現(xiàn)方法
前端添加的標(biāo)簽和方法:
驗(yàn)證碼:
$(function () { $("#imgValidateCode").click(function () { DoFresh(); }); DoFresh(); }) function DoFresh() { var img = $("#imgValidateCode"); img.attr("src", "VerifyCode.aspx?random=" + Math.random()); } //添加的方法,src是生成圖片的aspx的地址
然后在項(xiàng)目中在新建一個(gè)VerifyCode.aspx,下面是aspx的代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="VerifyCode.aspx.cs" Inherits="Form.VerifyCode" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> </div> </form> </body> </html>
接著是aspx.cs的代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Drawing; namespace Form { public partial class VerifyCode : System.Web.UI.Page { public static string HZ; /// <summary> /// 驗(yàn)證碼的最大長(zhǎng)度 /// </summary> public int MaxLength { get { return 10; } } /// <summary> /// 驗(yàn)證碼的最小長(zhǎng)度 /// </summary> public int MinLength { get { return 1; } } protected void Page_Load(object sender, EventArgs e) { string[] str = CreateValidateNumber(4); string strcode = string.Empty; for (int i = 0; i < str.Length; i++) { strcode += str[i]; } CreateCheckCodeImage(str); HZ = strcode; Response.Write(HZ); //驗(yàn)證碼存入session Session["ValidateCode"] = HZ; } /// <summary> /// 生成驗(yàn)證碼 /// </summary> /// <param name="length">指定驗(yàn)證碼的長(zhǎng)度</param> /// <returns>驗(yàn)證碼</returns> public string[] CreateValidateNumber(int length) { string Vchar = "1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,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,P,Q" + ",R,S,T,U,V,W,X,Y,Z"; string[] VcArray = Vchar.Split(new Char[] { ',' });//拆分成數(shù)組 string[] num = new string[length]; int temp = -1;//記錄上次隨機(jī)數(shù)值,盡量避避免生產(chǎn)幾個(gè)一樣的隨機(jī)數(shù) Random rand = new Random(); //采用一個(gè)簡(jiǎn)單的算法以保證生成隨機(jī)數(shù)的不同 for (int i = 1; i < length + 1; i++) { if (temp != -1) { rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks)); } int t = rand.Next(VcArray.Length-1); if (temp != -1 && temp == t) { return CreateValidateNumber(length); } temp = t; num[i - 1] = VcArray[t]; //num.SetValue(VcArray[t]); //VNum += VcArray[t]; } return num; } private void CreateCheckCodeImage(string[] checkCode) { if (checkCode == null || checkCode.Length <= 0) return; System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 32.5)), 60); System.Drawing.Graphics g = Graphics.FromImage(image); try { //生成隨機(jī)生成器 Random random = new Random(); //清空?qǐng)D片背景色 g.Clear(Color.White); //定義顏色 Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }; //畫圖片的背景噪音線 for (int i = 0; i < 25; i++) { int cindex = random.Next(7); int findex = random.Next(5); int x1 = random.Next(image.Width); int x2 = random.Next(image.Width); int y1 = random.Next(image.Height); int y2 = random.Next(image.Height); // g.DrawLine(new Pen(c[cindex]), x1, y1, x2, y2); } //定義字體 string[] f = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋體" }; for (int k = 0; k <= checkCode.Length - 1; k++) { int cindex = random.Next(7); int findex = random.Next(5); Font drawFont = new Font(f[findex], 26, (System.Drawing.FontStyle.Bold)); SolidBrush drawBrush = new SolidBrush(c[cindex]); float x = 5.0F; float y = 0.0F; float width = 42.0F; float height = 48.0F; int sjx = random.Next(10); int sjy = random.Next(image.Height - (int)height); RectangleF drawRect = new RectangleF(x + sjx + (k * 25), y + sjy, width, height); StringFormat drawFormat = new StringFormat(); drawFormat.Alignment = StringAlignment.Center; g.DrawString(checkCode[k], drawFont, drawBrush, drawRect, drawFormat); } //畫圖片的前景噪音點(diǎn) for (int i = 0; i < 500; i++) { int x = random.Next(image.Width); int y = random.Next(image.Height); image.SetPixel(x, y, Color.FromArgb(random.Next())); } int cindex1 = random.Next(7); //畫圖片的邊框線 g.DrawRectangle(new Pen(c[cindex1]), 0, 0, image.Width - 1, image.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent(); Response.ContentType = "image/Gif"; Response.BinaryWrite(ms.ToArray()); } finally { g.Dispose(); image.Dispose(); } } } }
于是!就可以生成驗(yàn)證碼了,然后自己再把編寫驗(yàn)證版的判斷邏輯寫好就可以啦!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Asp.net Web Api實(shí)現(xiàn)圖片點(diǎn)擊式圖片驗(yàn)證碼功能
- asp.net點(diǎn)選驗(yàn)證碼實(shí)現(xiàn)思路分享 (附demo)
- ASP.NET中畫圖形驗(yàn)證碼的實(shí)現(xiàn)代碼
- Asp.net開(kāi)發(fā)之webform圖片水印和圖片驗(yàn)證碼的實(shí)現(xiàn)方法
- asp.net之生成驗(yàn)證碼的方法集錦(一)
- 如何使用ASP.NET制作簡(jiǎn)單的驗(yàn)證碼
- asp.net驗(yàn)證碼圖片生成示例
- ASP.NET 實(shí)現(xiàn)驗(yàn)證碼以及刷新驗(yàn)證碼的小例子
- Asp.net實(shí)現(xiàn)手寫驗(yàn)證碼的操作代碼
相關(guān)文章
Aspose.Cells 讀取受保護(hù)有密碼的Excel文件
這篇文章主要介紹了Aspose.Cells 讀取受保護(hù)有密碼的Excel文件,簡(jiǎn)單實(shí)用,需要的朋友可以參考下。2016-06-06WPF開(kāi)發(fā)之利用DrawingVisual繪制高性能曲線圖
通過(guò)WPF實(shí)現(xiàn)大數(shù)據(jù)曲線圖時(shí),如果用最基礎(chǔ)的Canvas來(lái)實(shí)現(xiàn),性能堪憂。所以本文將利用DrawingVisual繪制高性能曲線圖,感興趣的可以了解一下2022-02-02ASP.NET?Core使用EF創(chuàng)建關(guān)系模型
這篇文章介紹了ASP.NET?Core使用EF創(chuàng)建關(guān)系模型的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04win8/8.1系統(tǒng)安裝.net framework 3.5出現(xiàn)0x800F0906代碼錯(cuò)誤的解決方法
這篇文章主要為大家詳細(xì)介紹了win8/8.1系統(tǒng)安裝.net framework 3.5出現(xiàn)0x800F0906代碼錯(cuò)誤的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01ASP.NET MVC使用EasyUI的datagrid多選提交保存教程
ASP.NET MVC使用EasyUI的datagrid多選提交保存教程,需要的朋友可以參考下。2011-12-12asp.net中JavaScript數(shù)據(jù)驗(yàn)證實(shí)現(xiàn)代碼
我對(duì)JavaScript一直不了解。常常為了一點(diǎn)點(diǎn)的數(shù)據(jù)驗(yàn)證和無(wú)刷新就去動(dòng)用AJAX,實(shí)在不爽——有點(diǎn)殺雞用牛刀的感覺(jué)。2010-05-0512306動(dòng)態(tài)驗(yàn)證碼啟發(fā)之ASP.NET實(shí)現(xiàn)動(dòng)態(tài)GIF驗(yàn)證碼(附源碼)
這篇文章主要介紹了受到12306動(dòng)態(tài)驗(yàn)證碼啟發(fā),實(shí)現(xiàn)ASP.NET動(dòng)態(tài)GIF驗(yàn)證碼,需要的朋友可以參考下2015-08-08在asp.net中操作sql server數(shù)據(jù)庫(kù)的一些小技巧
在asp.net中操作sql server數(shù)據(jù)庫(kù)的一些小技巧...2006-09-09