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

简介:

先建一个checkcode.aspx.

进入checkcode.aspx.cs

None.gif using System;
None.gif using System.Data;
None.gif using System.Configuration;
None.gif using System.Web;
None.gif using System.Web.Security;
None.gif using System.Web.UI;
None.gif using System.Web.UI.WebControls;
None.gif using System.Web.UI.WebControls.WebParts;
None.gif using System.Web.UI.HtmlControls;
None.gif
None.gif using System.Text;   // 添加引用
None.gif
using System.Drawing;   // 添加引用
None.gif

None.gif public 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 < 1; 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 < 50; 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="确定" OnClick="Button1_Click" />


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

进入default.aspx.cs

 

None.gif protected  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    }


自己看看!!!  
目录
相关文章
|
3月前
|
C# 开发者
C# 9.0中的模块初始化器:程序启动的新控制点
【1月更文挑战第14天】本文介绍了C# 9.0中引入的新特性——模块初始化器(Module initializers)。模块初始化器允许开发者在程序集加载时执行特定代码,为类型初始化提供了更细粒度的控制。文章详细阐述了模块初始化器的语法、用途以及与传统类型初始化器的区别,并通过示例代码展示了如何在实际项目中应用这一新特性。
|
3月前
|
编译器 C# 开发者
C# 9.0中的顶级语句:简化程序入口的新特性
【1月更文挑战第13天】本文介绍了C# 9.0中引入的顶级语句(Top-level statements)特性,该特性允许开发者在不使用传统的类和方法结构的情况下编写简洁的程序入口代码。文章详细阐述了顶级语句的语法、使用场景以及与传统程序结构的区别,并通过示例代码展示了其在实际应用中的便捷性。
|
6月前
|
开发框架 .NET C#
利用WinDbg分析C#程序产生的转储文件
利用WinDbg分析C#程序产生的转储文件
|
6月前
|
C# C++
VS调试C#程序产生的dump
VS调试C#程序产生的dump
|
6月前
|
C#
C#程序Debug文件夹可以运行,无法调试
C#程序Debug文件夹可以运行,无法调试
|
29天前
|
Java C# 开发工具
第一个C#程序
第一个C#程序
12 0
|
1月前
|
数据采集 存储 C#
抓取Instagram数据:Fizzler库带您进入C#程序的世界
在当今数字化的世界中,数据是无价之宝。社交媒体平台如Instagram成为了用户分享照片、视频和故事的热门场所。作为开发人员,我们可以利用爬虫技术来抓取这些平台上的数据,进行分析、挖掘和应用。本文将介绍如何使用C#编写一个简单的Instagram爬虫程序,使用Fizzler库来解析HTML页面,同时利用代理IP技术提高采集效率。
抓取Instagram数据:Fizzler库带您进入C#程序的世界
|
5月前
|
开发框架 网络协议 前端开发
一个对C#程序混淆加密,小巧但够用的小工具
一个对C#程序混淆加密,小巧但够用的小工具
83 1
|
7月前
|
C#
C#开源的虚拟桌宠模拟器,可以内置到任何WPF应用程序 - VPet
C#开源的虚拟桌宠模拟器,可以内置到任何WPF应用程序 - VPet
|
3月前
|
开发框架 前端开发 小程序
分享46个ASP.NET博客程序源码,总有一款适合您
分享46个ASP.NET博客程序源码,总有一款适合您
24 0