1、写一个验证码,前台什么都不用写,这是CreateVerificationCode_Page.aspx.cs后台代码
1 using System; 2 using System.Collections; 3 using System.Configuration; 4 using System.Data; 5 using System.Linq; 6 using System.Web; 7 using System.Web.Security; 8 using System.Web.UI; 9 using System.Web.UI.HtmlControls; 10 using System.Web.UI.WebControls; 11 using System.Web.UI.WebControls.WebParts; 12 using System.Xml.Linq; 13 using System.Drawing; 14 15 namespace 验证码 16 { 17 public partial class CreateVerificationCode_Page : System.Web.UI.Page 18 { 19 protected void Page_Load(object sender, EventArgs e) 20 { 21 // 在此处放置用户代码以初始化页面 22 CreateCheckCodeImage(GenerateCheckCode(5));//charNum=5, 23 Session["CheckCode"] = GenerateCheckCode(5); 24 } 25 26 #region Web 窗体设计器生成的代码 27 override protected void OnInit(EventArgs e) 28 { 29 // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 30 InitializeComponent(); 31 base.OnInit(e); 32 } 33 34 /// <summary> 35 /// 设计器支持所需的方法 - 不要使用代码编辑器修改此方法的内容。 36 /// </summary> 37 private void InitializeComponent() 38 { 39 this.Load += new EventHandler(Page_Load); 40 } 41 #endregion 42 43 private string GenerateCheckCode( int charNum)//charNum是验证码要显示的多少个字母或数字 44 { 45 int number; 46 char code; 47 string checkCode = String.Empty; 48 Random random = new Random(); 49 for (int i = 0; i < charNum; i++) 50 { 51 number = random.Next(); 52 if (number % 2 == 0)//转换成ASCII码得到相加后的数 再转换成字符char 53 { 54 int x = number % 10; 55 if (x == 0 || x == 1) 56 { 57 code = (char)('0' + (char)(3)); 58 } 59 else 60 { 61 code = (char)('0' + (char)(number % 10)); 62 } 63 } 64 else 65 { 66 int y = number % 26; 67 if (y == 8 || y == 14) 68 { 69 code = (char)('A' + (char)(0)); 70 } 71 else 72 { 73 code = (char)('A' + (char)(number % 26)); 74 } 75 } 76 checkCode += code.ToString(); 77 } 78 // Response.Cookies.Add(new HttpCookie("CheckCode", checkCode)); 79 return checkCode; 80 } 81 82 private void CreateCheckCodeImage(string checkCode) 83 { 84 if (checkCode == null || checkCode.Trim() == String.Empty) 85 { 86 return; 87 } 88 System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 20); Graphics g = Graphics.FromImage(image); 89 try 90 { 91 Random random = new Random();//生成随机生成器 92 g.Clear(Color.White);//清空图片背景色 93 for (int i = 0; i < 2; i++)//画图片的背景噪音线 94 { 95 int x1 = random.Next(image.Width); 96 int x2 = random.Next(image.Width); 97 int y1 = random.Next(image.Height); 98 int y2 = random.Next(image.Height); 99 g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2); 100 } 101 Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold)); 102 System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true); 103 g.DrawString(checkCode, font, brush, 2, 2); 104 for (int i = 0; i < 100; i++)//画图片的前景噪音点 105 { 106 int x = random.Next(image.Width); 107 int y = random.Next(image.Height); 108 image.SetPixel(x, y, Color.FromArgb(random.Next())); 109 } 110 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);//画图片的边框线 111 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 112 image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); 113 Response.ClearContent(); 114 Response.ContentType = "image/Gif"; 115 Response.BinaryWrite(ms.ToArray()); 116 } 117 finally 118 { 119 g.Dispose(); 120 image.Dispose(); 121 } 122 } 123 124 } 125 }
2、用到验证码的页面
(1)Login_Page.aspx前台
1 <script language="javascript">//看不清换一个实现 2 function change() 3 { 4 var img = document.getElementById("imgCode"); 5 img.src=img.src+'?'; 6 } 7 </script> 8 9 <tr> 10 <td align="right">VerificationCode:</td> 11 <td><input type="text" runat="server" id="txtCode" style="128px" maxlength="4" /></td> 12 <td> 13 <table> 14 <tr> 15 <td> 16 <img alt='' src="CreateVerificationCode_Page.aspx" id="imgCode"/> 17 </td> 18 <td> 19 <a href="javascript:change();" style="font-size:12px">NotClear?</a>//看不清换一个实现 20 </td> 21 </tr>
(2)Login_Page.aspx.cs后台
1 protected void btnLogin_Click(object sender, EventArgs e)//点击登录验证输入的验证码是否有误 2 { 3 string checkcode = Session["CheckCode"].ToString(); 4 if (txtCode.Value.Equals(checkcode, StringComparison.OrdinalIgnoreCase))//判断输入的与验证码是否相等 5 { 6 Response.Write("<script>alert('Verification code input correct!')</script>"); 7 } 8 else 9 { 10 Page.ClientScript.RegisterClientScriptBlock(ClientScript.GetType(), "myscript", "<script>alert('Verification code input incorrect!')</script>"); 11 } 12 }