当前位置: 代码迷 >> ASP.NET >> 高手帮小弟我看看www.haoyao.net的验证码该如何做?老有垃圾信息
  详细解决方案

高手帮小弟我看看www.haoyao.net的验证码该如何做?老有垃圾信息

热度:7815   发布时间:2013-02-25 00:00:00.0
高手帮我看看www.haoyao.net的验证码该怎么做?老有垃圾信息
高手帮我看看www.haoyao.net的验证码该怎么做?老有垃圾信息,这个网站是我们公司的,最近一段时间来由群发信息的,网站属于我们技术人员独立开发的。但是在验证码上可能防范不够,很容有垃圾、群发信息,那么如何制作验证码能够彻底防止垃圾信息呢?

------解决方案--------------------------------------------------------
cookie 记录时间和IP
检查程序
------解决方案--------------------------------------------------------
这是我的验证码生成,调用的时候如下
验证码:
&nbsp;<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/ImageUrl.aspx" CssClass="img" />
这里的ImageUrl.aspx就是下面的页面,页面上不用写任何东西,只需要将以下代码粘贴到后台即可,当然你也可以根据自己的需求多加些干扰线
C# code
using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Drawing;using System.Drawing.Drawing2D;public partial class ImageUrl : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {               GetCheckCodeImage(GetCode(5));    }    //产生随机字符串     private string GetCode(int num)    {        string[] source ={ "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",             "K", "L", "M", "N", "O","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", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y","z"};        string code = "";        Random rd = new Random();        for (int i = 0; i < num; i++)        {                        code += source[rd.Next(0, source.Length)];        }        Session["ValidateCode"] = code;//将字符串保存到Session中,以便需要时进行验证          return code;    }    #region    验证码    /// <summary>    /// 生成验证码    /// </summary>    /// <param name="checkCode"></param>    private void GetCheckCodeImage(string checkCode)    {        if (checkCode == null || checkCode.Trim() == String.Empty) return;        System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 18);        Graphics g = Graphics.FromImage(image);        try        {            Random random = new Random();     //生成随机生成器             g.Clear(Color.White);            //清空图片背景色            for (int i = 0; i < 5; i++)     //画图片的背景噪音线            {                int x1 = random.Next(image.Width);                int x2 = random.Next(image.Width);                int y1 = random.Next(image.Height);                int y2 = random.Next(image.Height);                if (i / 2 == 0)                {                    g.DrawLine(new Pen(Color.Red), x1, y1, x2, y2);                }                else                {                    g.DrawLine(new Pen(Color.Blue), x1, y1, x2, y2);                }            }            Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));            LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 3.6f, true);            g.DrawString(checkCode, font, brush, 2, 2);            //画图片的前景噪音点            for (int i = 0; i < 60; i++)            {                int x = random.Next(image.Width);                int y = random.Next(image.Height);                image.SetPixel(x, y, Color.FromArgb(random.Next()));            }            //画图片的边框线            g.DrawRectangle(new Pen(Color.Silver), 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();        }    }    #endregion}