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" οnclick="this.src='HandlerVerification.ashx?aaa='+new Date()" />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" οnclick="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的网址是没变化的,故不会重新提交给服务器,所以
    ///  我们可以给服务器传一个键对值,但这个值是要变化才行,要不浏览器认为网址还是没有变化,因为我们可以
    ///  在后面用时间的键对值 ,因为时间是在动的。
    ///  οnclick="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("验证码错误");
    }
}

 

转载于:https://www.cnblogs.com/yagzh2000/archive/2013/06/03/3114991.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值