写了个用户注册,想添加个验证码,我用自定义验证控制判断的,提交时一直出错误,最后试了下这样根本不可以用,想问下验证码的正确使用方法是什么样的呢,最好用个实例,谢了·

解决方案 »

  1.   

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="showcode.aspx.cs" Inherits="showcode" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    showcode.aspx
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Image ID="Image1" runat="server" ImageUrl="CreateCheckCode.aspx" />
            
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="刷新验证码" />
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />
        </div>
        </form>
    </body>
    </html>showcode.aspx.csusing 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;public partial class showcode : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
                        
        }   
        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("showcode.aspx");
        }
        protected void Button2_Click(object sender, EventArgs e)
        {        if(this.TextBox1.Text.Equals(Session["uid"].ToString()))
            {
                Response.Write("验证成功!");
            }
            else
            {
                Response.Write("验证失败!");
            }
        }
    }
      

  2.   

    CreateCheckCode.aspx<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CreateCheckCode.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
        </div>
        </form>
    </body>
    </html>CreateCheckCode.aspx.csusing 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.Design;
    using System.Drawing.Drawing2D;
    using System.Drawing.Printing;
    using System.Drawing.Imaging;
    using System.IO;
    public partial class _Default : System.Web.UI.Page
    {
        private string CreateCheckCodeString()
        { //定义用于验证码的字符数组
            char[] AllCheckCodeArray ={ '0','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'};
            //定义验证码字符串
            string randomcode = "";
            Random rd = new Random();
            //生成4位验证码字符串
            for (int i = 0; i < 4; i++)
                randomcode += AllCheckCodeArray[rd.Next(AllCheckCodeArray.Length)];
            return randomcode;
        }
        //生成验证码图片
        protected void Page_Load(object sender, EventArgs e)
        {
            //定义图片的宽度
            int ImageWidth = 55;
            //定义图片高度
            int ImageHeigh = 22;
            //定义字体,用于绘制文字
            Font font = new Font("Arial", 12, FontStyle.Bold);
            //定义画笔,用于绘制文字
            Brush brush = new SolidBrush(Color.Black);        //定义钢笔,用于绘制干扰线        Bitmap BitImage = new Bitmap(ImageWidth, ImageHeigh);
            //从图像获取一个绘画面
            Graphics graphics = Graphics.FromImage(BitImage);
            //清除整个绘图画面并用颜色填充
            graphics.Clear(ColorTranslator.FromHtml("#F0F0F0"));//这里从HTML代码获取color对象
            //定义文字的绘制矩形区域
            RectangleF rect = new RectangleF(5, 2, ImageWidth, ImageHeigh);
            string key = CreateCheckCodeString();
            Session["uid"] = key;
            graphics.DrawEllipse(new Pen(Color.Blue),0,0,50,20);
            graphics.DrawString(key, font, brush, rect);
            //保存图片为gif格式
            BitImage.Save(Response.OutputStream, ImageFormat.Gif);
            //释放对象
            graphics.Dispose();
            BitImage.Dispose();
        }
    }