ASP.NET 关于验证码

/***********下面这段代码是写在ValidateImage.apsx里的,它的功能是处理传过来的验证值及创建图形****************/

//获取图片路径
    private readonly string ImagePath = "../skin/images/frame/validator.jpg";
    //保存传过来的验证值
    private static string sValidator = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if(Request.Params["Validator"] != null)
        {
            sValidator = Request.Params["Validator"].ToString();
      

        //创建BMP位图
        Bitmap bimMapImage = new Bitmap(Server.MapPath(ImagePath));
        Graphics graphicsImage = Graphics.FromImage(bimMapImage);

       

    //声明随机对象

        Random random = new Random();

 

        //清空图片背景色

        graphicsImage.Clear(Color.White);

 

        //画图片的背景噪音线

        for (int i = 0; i < 25; i++)

        {

 

            int x1 = random.Next(bimMapImage.Width);

            int x2 = random.Next(bimMapImage.Width);

            int y1 = random.Next(bimMapImage.Height);

            int y2 = random.Next(bimMapImage.Height);

 

 

            graphicsImage.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);

        }

 

        //画图片的前景噪音点     

        for (int i = 0; i < 500; i++)           

        {

            int x = random.Next(bimMapImage.Width); int y = random.Next(bimMapImage.Height);

 

 

            bimMapImage.SetPixel(x, y, Color.FromArgb(random.Next()));

        }

 

 

        //画图片的边框线            

        graphicsImage.DrawRectangle(new Pen(Color.Silver), 0, 0, bimMapImage.Width - 1, bimMapImage.Height - 1);

        // 设置画笔的输出模式
        graphicsImage.SmoothingMode = SmoothingMode.AntiAlias;

        //添加文本字符串
        SolidBrush sb = new SolidBrush(getColor());
        graphicsImage.DrawString(sValidator, new Font("Arial", 20, FontStyle.Bold), sb, new Point(0, 0));

        //设置图像输出的格式
        Response.ContentType = "image/jpeg";

        //保存数据流
        bimMapImage.Save(Response.OutputStream,ImageFormat.Jpeg);

        //释放占用的资源
        graphicsImage.Dispose();
        bimMapImage.Dispose();
    }

    //产生验证码颜色
    private Color getColor()
    {
        Random r = new Random();
        return Color.FromArgb(r.Next(256), r.Next(256), r.Next(256));
    }

/*****************************这段代码是用来产生和判断验证码的************************************/

static private string sValidator = "";
    private readonly string sValidorImageUrl = "ValidateImage.aspx?Validator=";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //sValidator = GetRandomint();产生4位随机数的验证码
            sValidator = CheckNumber(); //产生len位 数字+字符(且带有颜色)的验证码
            ValidateImage.ImageUrl = sValidorImageUrl + sValidator;
        }
    }

    /// <summary>
    /// 获取4位的随机数
    /// </summary>
    /// <returns></returns>
    private string GetRandomint()
    {
        Random rd = new Random();
        return (rd.Next(1000,9999).ToString());
    }

     /// <summary>
    /// 获取len为随机验证值
    /// </summary>
    /// <returns></returns>

    public static string CheckNumber()
    {
        string checkcode = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int len = 5;
        string tmpstr = "";
        int iRandNum;
        Random rnd = new Random();
        for (int i = 0; i < len; i++)
        {
            iRandNum = rnd.Next(checkcode.Length);
            tmpstr += checkcode[iRandNum];
        }
        return tmpstr;
    }

    /// <summary>
    /// 管理员登陆
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
            //if (sValidator == Code.Text.Trim().ToString())  //这样写会区分大小写即必须和验证码一模一样
            if(sValidator.Equals(Code.Text.Trim().ToString(),StringComparison.OrdinalIgnoreCase))  //不区分大小写
            {

                  //当验证成功后.....这里的代码应该是判断用户是否登录成功

            }

            else
            {
                Response.Write("<script>alert('验证码错误');location.href='CX_login.aspx';</script>");
            }

     }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值