EntityFramework数据持久化------------验证码

简易版

视图代码

             <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Image ID="Image1" runat="server" ImageUrl="images/"/>//图片并不重要,原理类似于防盗
            <asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click"/>

验证接口代码

 public class ooooo : 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;

            context.Session["code"] = code;//将验证码放入session作用域

            Bitmap bitmap = new Bitmap(120, 30);
            Graphics graphics = Graphics.FromImage(bitmap);
            //绘制验证码
            graphics.DrawString(code, new Font("微软雅黑", 12, FontStyle.Bold), Brushes.Yellow, new Point(2, 2));
            bitmap.Save(context.Response.OutputStream, ImageFormat.Jpeg);
        }
     

详细版

视图代码

 <tr>
                <td>账号</td>
                <td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td><br /><br />
                <td>密码</td>
                <td><asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox></td><br /><br />
                <td>验证码</td>
                <td><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                    <asp:Image ID="Image1" runat="server" ImageUrl="images/1.jpg" />
                    <asp:LinkButton ID="LinkButton1" runat="server">刷新</asp:LinkButton>
                </td><br /><br />
            </tr>
            <asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click" />
        </div>
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

验证接口代码

public class VerificationCode : IHttpHandler, IRequiresSessionState//需要引用
{
public bool IsReusable => false;

    private Random RandomSeed = new Random();
    public void ProcessRequest(HttpContext context)
    {
        //供验证码使用的字符
        string strWord = "123456789QWERTYUIOPASDFGHJKLZXCVBNM";
        string NumStr = null;
        for (int i = 0; i < 5; i++)
        {
            NumStr += strWord[RandomSeed.Next(0, strWord.Length)];
        }
        //将验证码保存到Session中
        context.Session["vcode"] = NumStr.ToLower();
        CreateImages(context, NumStr);
    }
    private void CreateImages(HttpContext context, string checkCode)
    {
        int iwidth = (int)(checkCode.Length * 13);
        System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 22);
        Graphics g = Graphics.FromImage(image);
        g.Clear(Color.Wheat);
        //定义颜色
        Color[] c = {Color.Black,Color.Red,Color.DarkBlue,Color.Green,Color.Orange,
        Color.Brown,Color.DarkCyan,Color.Purple};
        //定义字体
        string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
        Random rand = new Random();
        //随机输出噪点
        for (int i = 0; i < 50; i++)
        {
            int x = rand.Next(image.Width);
            int y = rand.Next(image.Height);
            g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);
        }
        //输出不同颜色的验证码字符
        for(int i = 0; i < checkCode.Length; i++)
        {
            int cindex = rand.Next(7);
            int findex = rand.Next(5);
            Font f = new System.Drawing.Font(font[findex], 10, System.Drawing.FontStyle.Bold);
            Brush b = new System.Drawing.SolidBrush(c[cindex]);
            int ii = 4;
            if ((i + 1) % 2 == 0)
            {
                ii = 2;
            }
            g.DrawString(checkCode.Substring(i, 1), f, b, 2 + (i * 12), ii);
        }
        //画一个边框
        g.DrawRectangle(new Pen(ColorTranslator.FromHtml("#CCCCCC"), 0), 0, 0,
            image.Width - 1, image.Height - 1);
        
       //输出到浏览器
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        context.Response.ClearContent();
        context.Response.ContentType = "image/gif";
        context.Response.BinaryWrite(ms.ToArray());
        g.Dispose();
        image.Dispose();
        }

引用

 <system.webServer>
    <handlers>
      <add verb="*" path="images/*" name="handler" 
      type="T7.VerificationCode"/>//必须要对应自己的项目.接口名
    </handlers>
  </system.webServer>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值