欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

asp.net登录验证码

程序员文章站 2022-05-13 09:50:46
...

前台代码

<form method="POST" action="CheckUser.aspx" id="fromLogin">            
            <table>
                <tr>
                    <td><img src="ValidCode.aspx" alt='看不清楚,双击图片换一张。'ondblclick=" this.src = 'ValidCode.aspx?flag=' + Math.random() " border="1" height="44" width="126"/></td>
                </tr>                
            </table>
        </form>

验证码生成页:ValidCode.aspx

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Web.UI;

namespace CST.WebApp
{
    public partial class ValidCode : Page
    {
        private readonly Random ran = new Random();

        protected void Page_Load(object sender, EventArgs e)
        {
            string str = getRandomValidate(5);
            Session["check"] = str;
            //这一步是为了将验证码写入Session,进行验证,不能缺省,也可一使用cookie
            getImageValidate(str);
        }

        //得到随机字符串,长度自己定义
        private string getRandomValidate(int len)
        {
            int num;
            int tem;
            string rtuStr = "";
            for (int i = 0; i < len; i++)
            {
                num = ran.Next();
                if (i%2 == 0)
                {
                    tem = num%10 + '0'; //生成数字 
                }
                else
                {
                    tem = num%26 + 'A'; //生成字符
                }
                rtuStr += Convert.ToChar(tem).ToString() + " ";
            }
            return rtuStr;
        }

        //生成图像
        private void getImageValidate(string strValue)
        {
            //string str = "OO00"; //前两个为字母O,后两个为数字0
            //int width = Convert.ToInt32(strValue.Length*12); //计算图像宽度
            // var img = new Bitmap(width, 141);
            var img = new Bitmap(150, 42);
            Graphics gfc = Graphics.FromImage(img); //产生Graphics对象,进行画图
            gfc.Clear(Color.White);
            drawLine(gfc, img);
            //写验证码,需要定义Font
            var font = new Font("Brush Script Std", 16);
            var brush =
                new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.DarkOrchid, Color.Blue, 1.5f,
                                        true);
            gfc.DrawString(strValue, font, brush, 20, 9);
            drawPoint(img);
            gfc.DrawRectangle(new Pen(Color.DarkGray), 0, 0, img.Width - 1, img.Height - 1);
            //将图像添加到页面
            var ms = new MemoryStream();
            img.Save(ms, ImageFormat.Gif);
            //更改Http头
            Response.ClearContent();
            Response.ContentType = "image/gif";
            Response.BinaryWrite(ms.ToArray());
            //Dispose
            gfc.Dispose();
            img.Dispose();
            Response.End();
        }

        private void drawLine(Graphics gfc, Bitmap img)
        {
            //选择画10条线,也可以增加,也可以不要线,只要随机杂点即可
            for (int i = 0; i < 10; i++)
            {
                int x1 = ran.Next(img.Width);
                int y1 = ran.Next(img.Height);
                int x2 = ran.Next(img.Width);
                int y2 = ran.Next(img.Height);
                gfc.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); //注意画笔一定要浅颜色,否则验证码看不清楚
            }
        }

        private void drawPoint(Bitmap img)
        {
            int col = ran.Next(); //在一次的图片中杂店颜色相同
            for (int i = 0; i < 100; i++)
            {
                int x = ran.Next(img.Width);
                int y = ran.Next(img.Height);
                img.SetPixel(x, y, Color.FromArgb(col));
            }
        }
    }
}


转载于:https://my.oschina.net/mandy4107/blog/480785