zoukankan      html  css  js  c++  java
  • 9. 验证码原理

    为防止暴力操作,现在很多网站都会用图片验证码来防止这样的操作。

    图片验证码的原理是:

       服务器处理程序产生随机数字并把这些数字绘制于图片中,同时把数字保存于session中,客户端根据显示的图片信息把验证码输入到编辑框中并提交到服务端,服务端根据编辑框中的数据和session的值进行比较即可。

    1.建立一般处理程序

    <%@ WebHandler Language="C#" Class="HandlerVerification" %>
    
    using System;
    using System.Web;
    using System.Drawing;
    using System.Drawing.Imaging;
    /// <summary>
    /// 验证码原理:
    ///   在服务端的处理程序时创建图片,并在图片上画上随机产生的字体并保存于session中,客户端
    ///   再读写session并与输入的字符进行比较,如果相等就是人工输入 ,如果不正确就是机器人输入 。
    ///   用一般处理程序时一定要继承IRequiresSessionState接口,且专门处理的类型为image/JPEG,即ContentType="image/JPEG"
    ///   用一般处理程序时创建session需要用HttpContext.Current.Session才行
    ///   把图片保存于Response.OutputStream的流中即可。
    /// </summary>
    public class HandlerVerification : IHttpHandler,System.Web.SessionState.IRequiresSessionState{
        
        public void ProcessRequest (HttpContext context) {
            context.Response.ContentType = "image/JPEG";
            using(Bitmap bmp=new Bitmap(100,50))
            {
                using(Graphics g=Graphics.FromImage(bmp))
                {
                    Random rand = new Random();
                    int value = rand.Next(1000, 10000);
                    string svalue = value.ToString();
                    HttpContext.Current.Session["VerCode"] = svalue;
                    Font font = new Font("宋体", 20, FontStyle.Italic);                
                    Brush brush = Brushes.Yellow;
                    g.DrawString(svalue, font, brush, new PointF(10, 10));
                    bmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                }
            }
        }
     
        public bool IsReusable {
            get {
                return false;
            }
        }
    
    }

    2.客户端源码

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ClientVerification.aspx.cs" Inherits="ClientVerification" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="JScript.js" type="text/javascript"></script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
          <img src="HandlerVerification.ashx" onclick="this.src='HandlerVerification.ashx?aaa='+new Date()" />
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="验证" />
          
        </div>
        </form>
    </body>
    </html>


    button代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class ClientVerification : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        /// <summary>
        ///  为了更换图片显示,则需要点击图片,即它的click事件,则我们只需把img的src重新指定一下,但会
        ///  发现图片不动,因为传的HandlerVerification.ashx的网址是没变化的,故不会重新提交给服务器,所以
        ///  我们可以给服务器传一个键对值,但这个值是要变化才行,要不浏览器认为网址还是没有变化,因为我们可以
        ///  在后面用时间的键对值 ,因为时间是在动的。
        ///  onclick="this.src='HandlerVerification.ashx?aaa='+new Date()
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            string scode =Convert.ToString(Session["VerCode"]);
            if (TextBox1.Text == scode)
            {
                Response.Write("验证码正确");
            }
            else
                Response.Write("验证码错误");
        }
    }
  • 相关阅读:
    Spring MVC系列之Hello World(SpringBoot)(六)
    SpringBoot系列之注解@Autowired VS @Qualifier VS @Primary(五)
    SpringBoot系列之注解@Component VS @Bean(四)
    SpringBoot系列之@PropertySource和@Value注解(二)
    SpringBoot系列之入门篇(一)
    不要叫我,我会叫你(控制反转原理)
    EntityFramework Core 3多次Include导致查询性能低之解决方案
    EntityFramework Core 3.0查询
    Java入门系列之集合HashMap源码分析(十四)
    浅析性能测试策略及适用场景
  • 原文地址:https://www.cnblogs.com/yagzh2000/p/3114991.html
Copyright © 2011-2022 走看看