EF关于验证码制作

为什么要用到验证码?

答:有很多注册,回答和操作,或是做任务什么的是需要人去做的,但是有很多人会写一写脚本语言,让电脑自动执行,这就是作弊,所以程序员设计了验证码模式,而电脑并不能识别图片上那些横七竖八的数字或文字,这样人们就无法用脚本语言作弊了,必须亲自操作,杜绝了作弊
1.在电脑上添加一个ASP.NET web项目
2.在项目中添加一个后缀名为“.aspx”的web窗体。其中的代码如下:

<div>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Image ID="Image1" runat="server" ImageUrl="~/images/1.jpg"/>
            <br />
            <asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click" />
        </div>

3.添加一个文件夹,并添加好所需图片
4.添加好“VaidateHandler”类,并添加好数据,其代码如下:

public class vaidateHandler : IHttpHandler,IRequiresSessionState
    {
        public bool IsReusable => false;

        public void ProcessRequest(HttpContext context)
        {
            //定义一个随机对象
            Random random = new Random();
            int a = random.Next(0, 9);
            int b= random.Next(0, 9);
            int c = random.Next(0, 9);
            int d = random.Next(0, 9);
            int e = random.Next(0, 9);
            int f = random.Next(0, 9);
            string code = "";
            code = code + a + b + c + d + e + f;
            //将验证码放入session作用域
            context.Session["code"] = code;
            Bitmap bitmap = new Bitmap(120, 30);
            Graphics graphics = Graphics.FromImage(bitmap);
            //绘制验证码
            graphics.DrawString(code, new Font("微软雅黑", 12, FontStyle.Bold), Brushes.White, new Point(2, 2));
            graphics.Flush();
            bitmap.Save(context.Response.OutputStream, ImageFormat.Jpeg);
        }
    }

5.在MyCould类中实现好接口,并添加好数据。其代码如下:

public class MyCould : IHttpModule
    {
        public void Dispose()
        {
            
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += Context_BeginRequest;
            context.EndRequest += Context_EndRequest;
        }

        private void Context_EndRequest(object sender, EventArgs e)
        {
            HttpApplication context = sender as HttpApplication;
        }

        private void Context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication context = sender as HttpApplication;
        }
    }

6.web.config的界面添加好配置,其代码如下:

<system.webServer>
    <modules>
      <add name="modules1" type="website.MyCould"/>
    </modules>
    <handlers>
    //website是项目名称 type后面接的是website下面的一个类
      <add verb="*" name="h1" type="website.vaidateHandler" path="Images/*.jpg"/>
    </handlers>
  </system.webServer>

最后效果图如下:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值