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

本文介绍如何使用ASP.NET和C#生成包含随机字符串的验证码图片,包括添加噪声线和噪声点来增强安全性。通过Session存储随机字符串,以便后续验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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


  1 None.gif using  System;
  2 None.gif using  System.IO;
  3 None.gif using  System.Web.UI;
  4 None.gif using  System.Drawing;
  5 None.gif using  System.Drawing.Imaging;
  6 None.gif using  System.Drawing.Drawing2D;
  7 None.gif
  8 None.gif namespace  Web.Common
  9 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 10ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 11InBlock.gif    /// validate 的摘要说明。
 12ExpandedSubBlockEnd.gif    /// </summary>

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

 20InBlock.gif    
 21InBlock.gif        //绘图
 22InBlock.gif        private void DrawValidateCode(string strValidateCode,int intFgNoise,int intBgNoise)
 23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 24InBlock.gif            if(strValidateCode == null || strValidateCode.Trim() == String.Empty)
 25ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 26InBlock.gif                return;
 27ExpandedSubBlockEnd.gif            }

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

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

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

 75InBlock.gif                finally
 76ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 77InBlock.gif                    grpGraphics.Dispose();
 78InBlock.gif                    bmpImage.Dispose();
 79ExpandedSubBlockEnd.gif                }

 80ExpandedSubBlockEnd.gif            }

 81ExpandedSubBlockEnd.gif        }

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

 99InBlock.gif                else
100ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
101InBlock.gif                    chrCode = (char)('A' + (char)(intNumber % 26));//如果随机数是奇数 选择从[A-Z]
102ExpandedSubBlockEnd.gif                }

103InBlock.gif                strValidateCode += chrCode.ToString(); 
104ExpandedSubBlockEnd.gif            }

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

111InBlock.gif
112ContractedSubBlock.gifExpandedSubBlockStart.gif        Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
113InBlock.gif        override protected void OnInit(EventArgs e)
114ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
115InBlock.gif            //
116InBlock.gif            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
117InBlock.gif            //
118InBlock.gif            InitializeComponent();
119InBlock.gif            base.OnInit(e);
120ExpandedSubBlockEnd.gif        }

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

126InBlock.gif        private void InitializeComponent()
127ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{    
128InBlock.gif            this.Load += new System.EventHandler(this.Page_Load);
129InBlock.gif
130ExpandedSubBlockEnd.gif        }

131ExpandedSubBlockEnd.gif        #endregion

132ExpandedSubBlockEnd.gif    }

133InBlock.gif
134ExpandedBlockEnd.gif}

135 None.gif

转载于:https://www.cnblogs.com/Spring/archive/2006/11/04/549840.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值