zl程序教程

您现在的位置是:首页 >  后端

当前栏目

asp.net(c#)中文验证码程序

2023-09-27 14:27:56 时间
None.gifusing System;
None.gifusing System.Data;
None.gifusing System.Configuration;
None.gifusing System.Web;
None.gifusing System.Web.Security;
None.gifusing System.Web.UI;
None.gifusing System.Web.UI.WebControls;
None.gifusing System.Web.UI.WebControls.WebParts;
None.gifusing System.Web.UI.HtmlControls;
None.gif
None.gifusing System.Text;  //添加引用
None.gifusing System.Drawing;  //添加引用
None.gif
None.gifpublic partial class _Default : System.Web.UI.Page 
ExpandedBlockStart.gif{
InBlock.gif    protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gif    {
InBlock.gif        GraphicsImage(4);  //调用方法生成四位汉字验证码
ExpandedSubBlockEnd.gif    }
InBlock.gif
InBlock.gif    private object[] CreateString(int strlength)
ExpandedSubBlockStart.gif    {
InBlock.gif        //定义一个数组存储汉字编码的组成元素
ExpandedSubBlockStart.gif        string[] str = new string[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
InBlock.gif
InBlock.gif        Random ran = new Random();  //定义一个随机数对象
InBlock.gif        object[] bytes = new object[strlength];
InBlock.gif        for (int i = 0; i   strlength; i++)
ExpandedSubBlockStart.gif        {
InBlock.gif            //获取区位码第一位
InBlock.gif            int ran1 = ran.Next(11, 14);
InBlock.gif            string str1 = str[ran1].Trim();
InBlock.gif
InBlock.gif            //获取区位码第二位并防止数据重复
InBlock.gif            ran = new Random(ran1 * unchecked((int)DateTime.Now.Ticks) + i);
InBlock.gif            int ran2;
InBlock.gif            if (ran1 == 13)
ExpandedSubBlockStart.gif            {
InBlock.gif                ran2 = ran.Next(0, 7);
ExpandedSubBlockEnd.gif            }
InBlock.gif            else
ExpandedSubBlockStart.gif            {
InBlock.gif                ran2 = ran.Next(0, 16);
ExpandedSubBlockEnd.gif            }
InBlock.gif            string str2 = str[ran2].Trim();
InBlock.gif
InBlock.gif            //获取区位码第三位
InBlock.gif            ran = new Random(ran2 * unchecked((int)DateTime.Now.Ticks) + i);
InBlock.gif            int ran3 = ran.Next(10, 16);
InBlock.gif            string str3 = str[ran3].Trim();
InBlock.gif
InBlock.gif            //获取区位码第四位
InBlock.gif            ran = new Random(ran3 * unchecked((int)DateTime.Now.Ticks) + i);
InBlock.gif            int ran4;
InBlock.gif            if (ran3 == 10)
ExpandedSubBlockStart.gif            {
InBlock.gif                ran4 = ran.Next(1, 16);
ExpandedSubBlockEnd.gif            }
InBlock.gif            else if (ran3 == 15)
ExpandedSubBlockStart.gif            {
InBlock.gif                ran4 = ran.Next(0, 15);
ExpandedSubBlockEnd.gif            }
InBlock.gif            else
ExpandedSubBlockStart.gif            {
InBlock.gif                ran4 = ran.Next(0, 16);
ExpandedSubBlockEnd.gif            }
InBlock.gif            string str4 = str[ran4].Trim();
InBlock.gif
InBlock.gif            //定义字节变量存储产生的随机汉字区位码
InBlock.gif            byte byte1 = Convert.ToByte(str1 + str2, 16);
InBlock.gif            byte byte2 = Convert.ToByte(str3 + str4, 16);
InBlock.gif
ExpandedSubBlockStart.gif            byte[] stradd = new byte[] { byte1,byte2};
InBlock.gif            //将产生的汉字字节放入数组
InBlock.gif            bytes.SetValue(stradd, i);
ExpandedSubBlockEnd.gif        }
InBlock.gif        return bytes;
ExpandedSubBlockEnd.gif    }
InBlock.gif
InBlock.gif    private string GetString(int length)
ExpandedSubBlockStart.gif    {
InBlock.gif        Encoding gb = Encoding.GetEncoding("gb2312");
InBlock.gif        object[] bytes = CreateString(length);
InBlock.gif
InBlock.gif        //根据汉字字节解码出中文汉字
InBlock.gif        string str1 = gb.GetString((byte[])Convert.ChangeType(bytes[0], typeof(byte[])));
InBlock.gif        string str2 = gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[])));
InBlock.gif        string str3 = gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[])));
InBlock.gif        string str4 = gb.GetString((byte[])Convert.ChangeType(bytes[3], typeof(byte[])));
InBlock.gif
InBlock.gif        string str = str1 + str2 + str3 + str4;
InBlock.gif        Response.Cookies.Add(new HttpCookie("CheckCode", str));
InBlock.gif        return str;
ExpandedSubBlockEnd.gif    }
InBlock.gif
InBlock.gif    private void GraphicsImage(int length)
ExpandedSubBlockStart.gif    {
InBlock.gif        System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((GetString(length).Length * 22.5)), 22);
InBlock.gif        Graphics g = Graphics.FromImage(image);  //创建画布
InBlock.gif
InBlock.gif        try
ExpandedSubBlockStart.gif        {
InBlock.gif            //生成随机生成器
InBlock.gif            Random random = new Random();
InBlock.gif
InBlock.gif            //清空图片背景色
InBlock.gif            g.Clear(Color.White);
InBlock.gif
InBlock.gif            //画图片的背景噪音线
InBlock.gif            for (int i = 0; i   i++)
ExpandedSubBlockStart.gif            {
InBlock.gif                int x1 = random.Next(image.Width);
InBlock.gif                int x2 = random.Next(image.Width);
InBlock.gif                int y1 = random.Next(image.Height);
InBlock.gif                int y2 = random.Next(image.Height);
InBlock.gif
InBlock.gif                g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif            Font font = new System.Drawing.Font("Couriew New", 12, System.Drawing.FontStyle.Bold );
InBlock.gif            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush
InBlock.gif                (new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
InBlock.gif            g.DrawString(GetString(length), font, brush, 2, 2);
InBlock.gif
InBlock.gif            //画图片的前景噪音点
InBlock.gif            for (int i = 0; i   i++)
ExpandedSubBlockStart.gif            {
InBlock.gif                int x = random.Next(image.Width);
InBlock.gif                int y = random.Next(image.Height);
InBlock.gif
InBlock.gif                image.SetPixel(x, y, Color.FromArgb(random.Next()));
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif            //画图片的边框线
InBlock.gif            g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
InBlock.gif            System.IO.MemoryStream ms = new System.IO.MemoryStream();
InBlock.gif            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
InBlock.gif            Response.ClearContent();
InBlock.gif            Response.ContentType = "image/Gif";
InBlock.gif            Response.BinaryWrite(ms.ToArray());
ExpandedSubBlockEnd.gif        }
InBlock.gif        catch (Exception ms)
ExpandedSubBlockStart.gif        {
InBlock.gif            Response.Write(ms.Message);
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}
None.gif
None.gif 
None.gif
有耐心的就看完,没耐心的就直接复制吧!

注意里面的一句:
None.gifResponse.Cookies.Add(new HttpCookie("CheckCode", str));

是用Cookies保存的不是用Session大家注意点.

新建一个defaulta.aspx,

代码主要是

None.gif asp:TextBox ID="TextBox3" runat="server" Font-Size="9pt" Width="65px" /asp:TextBox
None.gif asp:Image ID="Image1" src="CheckCode.aspx" runat="server" Height="21px" Width="85px" /
None.gif asp:Button ID="Button1" runat="server" Font-Size="9pt" Text="确定"  /


注意上面只是贴出主要代码,只是一个文本框,一个验证码图片,一个确定按钮.

进入default.aspx.cs

 

None.gifprotected void Button1_Click(object sender, EventArgs e)
ExpandedBlockStart.gif    {
InBlock.gif        HttpCookie cookie = Request.Cookies["CheckCode"];
InBlock.gif        if (cookie.Value == this.TextBox3.Text.Trim())
ExpandedSubBlockStart.gif        {
InBlock.gif            Response.Write(" script alert(‘验证码正确!‘) /script
ExpandedSubBlockEnd.gif        }
InBlock.gif        else
ExpandedSubBlockStart.gif        {
InBlock.gif            Response.Write(" script alert(‘验证码错误!‘) /script
ExpandedSubBlockEnd.gif        }
ExpandedBlockEnd.gif    }

自己看看!!! 
ASP.NET ashx实现无刷新页面生成验证码 现在大部分网站登陆时都会要求输入验证码,在网上也看了一些范例,现在总结一下如何实现无刷新页面生成验证码。 实现方式: 2 Identifying Code: