随机验证码

(一)新建一个aspx网页,命名为RandomCode.aspx

     前台页面的网页标签只保留Page属性信息。其他都删去。

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RandomCode.aspx.cs" Inherits="Web.admin.RandomCode" %>

 

(二)后台代码如下:

代码
1 using System;
2  using System.Collections;
3  using System.Configuration;
4  using System.Data;
5  using System.Collections.Generic;
6  using System.Web;
7  using System.Web.UI;
8  using System.Web.UI.WebControls;
9  using System.Web.UI.WebControls.WebParts;
10  using System.Web.UI.HtmlControls;
11  using System.Drawing;
12
13  namespace Web.admin
14 {
15 public partial class RandomCode : System.Web.UI.Page
16 {
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 string checkCode = GetRandomCode(5);
20 Session["CheckCode"] = checkCode;
21 SetPageNoCache();
22 CreateImage(checkCode);
23 }
24
25 //获取验证码各个字母
26 private string GetRandomCode(int CodeCount)
27 {
28 string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,M,N,P,Q,R,S,T,U,W,X,Y,Z";
29 string[] allCharArray = allChar.Split(',');
30 string RandomCode = "";
31 int temp = -1;
32 Random rand = new Random();
33 for (int i = 0; i < CodeCount; i++)
34 {
35 if (temp != -1)
36 {
37 rand = new Random(temp * i * ((int)DateTime.Now.Ticks));
38 }
39 int t = rand.Next(33);
40 while (temp == t)
41 {
42 t = rand.Next(33);
43 }
44 temp = t;
45 RandomCode += allCharArray[t];
46 }
47 return RandomCode;
48 }
49
50 //页面设置没有缓存
51 private void SetPageNoCache()
52 {
53 Response.Buffer = true;
54 Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
55 Response.Expires = 0;
56 Response.CacheControl = "no-cache";
57 Response.AppendHeader("Pragma", "No-Cache");
58 }
59
60 //设置背景图片
61 private void CreateImage(string checkCode)
62 {
63 int iwidth = (int)(checkCode.Length * 14);
64 System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 30);
65 Graphics g = Graphics.FromImage(image);
66 Font f = new System.Drawing.Font("Arial",15);
67 Brush b = new System.Drawing.SolidBrush(Color.Black);
68 Brush r = new System.Drawing.SolidBrush(Color.FromArgb(166, 8, 8));
69 g.Clear(System.Drawing.ColorTranslator.FromHtml("#fff"));//背景色
70 char[] ch = checkCode.ToCharArray();
71 for (int i = 0; i < ch.Length; i++)
72 {
73 if (ch[i] >= '0' && ch[i] <= '9')
74 {
75 g.DrawString(ch[i].ToString(), f, r, 8 + (i * 12), 3);
76 }
77 else
78 {
79 g.DrawString(ch[i].ToString(), f, b, 3 + (i * 12), 3);
80 }
81 }
82 System.IO.MemoryStream ms = new System.IO.MemoryStream();
83 image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
84 Response.Cache.SetNoStore();
85 Response.ClearContent();
86 Response.ContentType = "image/Jpeg";
87 Response.BinaryWrite(ms.ToArray());
88 g.Dispose();
89 image.Dispose();
90 }
91 }
92 }

 



(三)以上完成了验证码页面的设置。接下来是实现在Login.aspx页面中调用。最终效果如下:

 

(四)部分Login.aspx的前台调用代码:

代码
1 <tr>
2 <td style="width:65px; color:White; font-size:14px;">
3 验证码:</td>
4 <td class="style1">
5 <asp:TextBox ID="txtYZM" runat="server" Width="53px"></asp:TextBox>
6 <img alt="验证码" src="RandomCode.aspx" title="点击" style=" margin-bottom:-5px; cursor:pointer;"
7 onclick="this.src='RandomCode.aspx?id='"+Math.random()+"'" />
8 </td>
9 </tr>

 


(五)后台的部分代码:

代码
1 if (Session["CheckCode"] != null)
2 {
3 if (txtYZM.Text.ToLower() == Session["CheckCode"].ToString().ToLower())
4 {
5 Response.Redirect("Default.aspx");
6 }
7 else
8 {
9 Common.MessageAlert.Alert(Page, "验证码错误");
10 }
11 }

 

使用的时候要把上面的代码嵌套进入自己的后台代码就可以用了。



 

 

posted @ 2010-11-24 18:50  莫萧  阅读(1856)  评论(0编辑  收藏  举报