ASP.NET with C#生成验证码的过程

简介: ASP.NET with C#生成验证码的过程 生成验证码的大概过程就是在后台取得一个随机的字符串,然后该随机字符串绘制成一幅图片,当然,图片可以加上噪声,防止基本上不会出现的N人分析图形数据获取和还原字符串。

ASP.NET with C#生成验证码的过程

生成验证码的大概过程就是在后台取得一个随机的字符串,然后该随机字符串绘制成一幅图片,当然,图片可以加上噪声,防止基本上不会出现的N人分析图形数据获取和还原字符串。

具体生成验证码的代码如下,在生成随机字符串的同时会将字符串设置到一个Session["ValidateCode"] 中,实用的时候只要得到用户返回值和Session值比较就可以得出填入的验证码是否一致了。


  1 img_a6339ee3e57d1d52bc7d02b338e15a60.gif using  System;
  2 img_a6339ee3e57d1d52bc7d02b338e15a60.gif using  System.IO;
  3 img_a6339ee3e57d1d52bc7d02b338e15a60.gif using  System.Web.UI;
  4 img_a6339ee3e57d1d52bc7d02b338e15a60.gif using  System.Drawing;
  5 img_a6339ee3e57d1d52bc7d02b338e15a60.gif using  System.Drawing.Imaging;
  6 img_a6339ee3e57d1d52bc7d02b338e15a60.gif using  System.Drawing.Drawing2D;
  7 img_a6339ee3e57d1d52bc7d02b338e15a60.gif
  8 img_a6339ee3e57d1d52bc7d02b338e15a60.gif namespace  Web.Common
  9 img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif {
 10img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    /**//// <summary>
 11img_33d02437d135341f0800e3d415312ae8.gif    /// validate 的摘要说明。
 12img_105a1e124122b2abcee4ea8e9f5108f3.gif    /// </summary>

 13img_33d02437d135341f0800e3d415312ae8.gif    public class Validate: System.Web.UI.Page
 14img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
 15img_33d02437d135341f0800e3d415312ae8.gif        private void Page_Load(object sender, EventArgs e)
 16img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
 17img_33d02437d135341f0800e3d415312ae8.gif            string strValidateCode = ValidateCode(6);//取得随机字符串,并设置Session值
 18img_33d02437d135341f0800e3d415312ae8.gif            DrawValidateCode(strValidateCode,50,100);//绘图
 19img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

 20img_33d02437d135341f0800e3d415312ae8.gif    
 21img_33d02437d135341f0800e3d415312ae8.gif        //绘图
 22img_33d02437d135341f0800e3d415312ae8.gif        private void DrawValidateCode(string strValidateCode,int intFgNoise,int intBgNoise)
 23img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
 24img_33d02437d135341f0800e3d415312ae8.gif            if(strValidateCode == null || strValidateCode.Trim() == String.Empty)
 25img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
 26img_33d02437d135341f0800e3d415312ae8.gif                return;
 27img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

 28img_33d02437d135341f0800e3d415312ae8.gif            else
 29img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
 30img_33d02437d135341f0800e3d415312ae8.gif                //建立一个位图文件 确立长宽
 31img_33d02437d135341f0800e3d415312ae8.gif                Bitmap bmpImage = new Bitmap((int)Math.Ceiling((strValidateCode.Length * 12.5)), 22);
 32img_33d02437d135341f0800e3d415312ae8.gif                Graphics grpGraphics = Graphics.FromImage(bmpImage);
 33img_33d02437d135341f0800e3d415312ae8.gif    
 34img_33d02437d135341f0800e3d415312ae8.gif                try
 35img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
 36img_33d02437d135341f0800e3d415312ae8.gif                    //生成随机生成器
 37img_33d02437d135341f0800e3d415312ae8.gif                    Random rndRandom = new Random();
 38img_33d02437d135341f0800e3d415312ae8.gif    
 39img_33d02437d135341f0800e3d415312ae8.gif                    //清空图片背景色
 40img_33d02437d135341f0800e3d415312ae8.gif                    grpGraphics.Clear(Color.White);
 41img_33d02437d135341f0800e3d415312ae8.gif    
 42img_33d02437d135341f0800e3d415312ae8.gif                    //画图片的背景噪音线
 43img_33d02437d135341f0800e3d415312ae8.gif                    for(int i=0; i<intBgNoise; i++)
 44img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                    img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
 45img_33d02437d135341f0800e3d415312ae8.gif                        int int_x1 = rndRandom.Next(bmpImage.Width);
 46img_33d02437d135341f0800e3d415312ae8.gif                        int int_x2 = rndRandom.Next(bmpImage.Width);
 47img_33d02437d135341f0800e3d415312ae8.gif                        int int_y1 = rndRandom.Next(bmpImage.Height);
 48img_33d02437d135341f0800e3d415312ae8.gif                        int int_y2 = rndRandom.Next(bmpImage.Height);
 49img_33d02437d135341f0800e3d415312ae8.gif    
 50img_33d02437d135341f0800e3d415312ae8.gif                        grpGraphics.DrawLine(new Pen(Color.Silver), int_x1, int_y1, int_x2, int_y2);
 51img_105a1e124122b2abcee4ea8e9f5108f3.gif                    }

 52img_33d02437d135341f0800e3d415312ae8.gif                    //把产生的随机数以字体的形式写入画面
 53img_33d02437d135341f0800e3d415312ae8.gif                    Font font = new Font("Arial"12, (FontStyle.Bold | FontStyle.Italic));
 54img_33d02437d135341f0800e3d415312ae8.gif                    LinearGradientBrush brhBrush = new LinearGradientBrush(new Rectangle(00, bmpImage.Width, bmpImage.Height), Color.Blue, Color.DarkRed, 1.2ftrue);
 55img_33d02437d135341f0800e3d415312ae8.gif                    grpGraphics.DrawString(strValidateCode, font, brhBrush, 22);
 56img_33d02437d135341f0800e3d415312ae8.gif    
 57img_33d02437d135341f0800e3d415312ae8.gif                    //画图片的前景噪音点
 58img_33d02437d135341f0800e3d415312ae8.gif                    for(int i=0; i<intFgNoise; i++)
 59img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                    img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
 60img_33d02437d135341f0800e3d415312ae8.gif                        int int_x = rndRandom.Next(bmpImage.Width);
 61img_33d02437d135341f0800e3d415312ae8.gif                        int int_y = rndRandom.Next(bmpImage.Height);
 62img_33d02437d135341f0800e3d415312ae8.gif    
 63img_33d02437d135341f0800e3d415312ae8.gif                        bmpImage.SetPixel(int_x, int_y, Color.FromArgb(rndRandom.Next()));
 64img_105a1e124122b2abcee4ea8e9f5108f3.gif                    }

 65img_33d02437d135341f0800e3d415312ae8.gif    
 66img_33d02437d135341f0800e3d415312ae8.gif                    //画图片的边框线
 67img_33d02437d135341f0800e3d415312ae8.gif                    grpGraphics.DrawRectangle(new Pen(Color.Silver), 00, bmpImage.Width - 1, bmpImage.Height - 1);
 68img_33d02437d135341f0800e3d415312ae8.gif    
 69img_33d02437d135341f0800e3d415312ae8.gif                    MemoryStream memsMemoryStream = new MemoryStream();
 70img_33d02437d135341f0800e3d415312ae8.gif                    bmpImage.Save(memsMemoryStream, ImageFormat.Gif);
 71img_33d02437d135341f0800e3d415312ae8.gif                    Response.ClearContent();
 72img_33d02437d135341f0800e3d415312ae8.gif                    Response.ContentType = "image/Gif";
 73img_33d02437d135341f0800e3d415312ae8.gif                    Response.BinaryWrite(memsMemoryStream.ToArray());
 74img_105a1e124122b2abcee4ea8e9f5108f3.gif                }

 75img_33d02437d135341f0800e3d415312ae8.gif                finally
 76img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
 77img_33d02437d135341f0800e3d415312ae8.gif                    grpGraphics.Dispose();
 78img_33d02437d135341f0800e3d415312ae8.gif                    bmpImage.Dispose();
 79img_105a1e124122b2abcee4ea8e9f5108f3.gif                }

 80img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

 81img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

 82img_33d02437d135341f0800e3d415312ae8.gif    
 83img_33d02437d135341f0800e3d415312ae8.gif        //取得随机字符串,并设置Session值
 84img_33d02437d135341f0800e3d415312ae8.gif        private string ValidateCode(int intLength)
 85img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
 86img_33d02437d135341f0800e3d415312ae8.gif            int intNumber;
 87img_33d02437d135341f0800e3d415312ae8.gif            char chrCode;
 88img_33d02437d135341f0800e3d415312ae8.gif            string strValidateCode = String.Empty;
 89img_33d02437d135341f0800e3d415312ae8.gif    
 90img_33d02437d135341f0800e3d415312ae8.gif            Random rndRandom = new Random();
 91img_33d02437d135341f0800e3d415312ae8.gif    
 92img_33d02437d135341f0800e3d415312ae8.gif            for(int i=0;i<intLength;i++)
 93img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
 94img_33d02437d135341f0800e3d415312ae8.gif                intNumber = rndRandom.Next();
 95img_33d02437d135341f0800e3d415312ae8.gif                if(intNumber % 2 == 0)
 96img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
 97img_33d02437d135341f0800e3d415312ae8.gif                    chrCode = (char)('0' + (char)(intNumber % 10));//如果随机数是偶数 取余
 98img_105a1e124122b2abcee4ea8e9f5108f3.gif                }

 99img_33d02437d135341f0800e3d415312ae8.gif                else
100img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
101img_33d02437d135341f0800e3d415312ae8.gif                    chrCode = (char)('A' + (char)(intNumber % 26));//如果随机数是奇数 选择从[A-Z]
102img_105a1e124122b2abcee4ea8e9f5108f3.gif                }

103img_33d02437d135341f0800e3d415312ae8.gif                strValidateCode += chrCode.ToString(); 
104img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

105img_33d02437d135341f0800e3d415312ae8.gif    
106img_33d02437d135341f0800e3d415312ae8.gif            Session["ValidateCode"= strValidateCode;//设置Session["ValidateCode"]
107img_33d02437d135341f0800e3d415312ae8.gif            //Response.Cookies.Add(new HttpCookie("strValidateCode",strValidateCode));
108img_33d02437d135341f0800e3d415312ae8.gif    
109img_33d02437d135341f0800e3d415312ae8.gif            return strValidateCode;
110img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

111img_33d02437d135341f0800e3d415312ae8.gif
112img_7a2b9a960ee9a98bfd25d306d55009f8.gifimg_2887d91d0594ef8793c1db92b8a1d545.gif        Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
113img_33d02437d135341f0800e3d415312ae8.gif        override protected void OnInit(EventArgs e)
114img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
115img_33d02437d135341f0800e3d415312ae8.gif            //
116img_33d02437d135341f0800e3d415312ae8.gif            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
117img_33d02437d135341f0800e3d415312ae8.gif            //
118img_33d02437d135341f0800e3d415312ae8.gif            InitializeComponent();
119img_33d02437d135341f0800e3d415312ae8.gif            base.OnInit(e);
120img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

121img_33d02437d135341f0800e3d415312ae8.gif        
122img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        /**//// <summary>
123img_33d02437d135341f0800e3d415312ae8.gif        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
124img_33d02437d135341f0800e3d415312ae8.gif        /// 此方法的内容。
125img_105a1e124122b2abcee4ea8e9f5108f3.gif        /// </summary>

126img_33d02437d135341f0800e3d415312ae8.gif        private void InitializeComponent()
127img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{    
128img_33d02437d135341f0800e3d415312ae8.gif            this.Load += new System.EventHandler(this.Page_Load);
129img_33d02437d135341f0800e3d415312ae8.gif
130img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

131img_105a1e124122b2abcee4ea8e9f5108f3.gif        #endregion

132img_105a1e124122b2abcee4ea8e9f5108f3.gif    }

133img_33d02437d135341f0800e3d415312ae8.gif
134img_05dd8d549cff04457a6366b0a7c9352a.gif}

135 img_a6339ee3e57d1d52bc7d02b338e15a60.gif

目录
相关文章
|
11月前
|
C# 开发工具
C#滑动拼图验证码实现笔记
C# 是一个现代的、通用的、面向对象的编程语言,它是由微软(Microsoft)开发的,由 Ecma 和 ISO 核准认可的。突发奇想,动手开发一个C#滑动拼图验证码,下面是我开发过程的记录。
C#滑动拼图验证码实现笔记
|
12月前
|
前端开发 C# 数据安全/隐私保护
C#验证码
验证码通常是为了区分用户是人还是计算机,也可以防止破解密码、刷票等恶意行为,而客户端上多数会用在关键操作上,比如购买、登录、注册等场景。现在验证码的种类样式也特别多,今天教大家如何用C#做出滑动拼图验证码吧~
C#验证码
|
C# 数据安全/隐私保护
C#实现手机发送验证码
C#实现手机发送验证码
411 0
|
开发框架 .NET
asp.net生成验证码并提交验证
asp.net生成验证码并提交验证
155 0

相关产品

  • 云迁移中心