zoukankan      html  css  js  c++  java
  • Ajax实现验证码功能

     越来越多的网站为了实现一个防恶意刷屏,而产生了一种验证码的东东,这个玩意儿发送到前台其实是一个图片,而关键对比数据保存在Session,在下在网上看到很多都是用VB.net写的,而C#实现的确不多,在下使用AJAXPRO技术加上位图操作实现对图片的重绘。。。写了一个完整的网页验证码的东东,希望看得明白的人喜欢。。。
    下面是源码:


      1。整个程序分为两个页面组成。

      第一个页面用来生成验证码的图像

      第二个页面是一个输入页面用来测试生成的验证码


    下面是Validator.aspx内容,这个页面是为了生成一个随机数的图像,里面有非常详细的注释

    /*下面是Validator.aspx.cs的代码*/ using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.Drawing.Text;
    using System.Drawing;
    using System.Text;
    using AjaxPro;
    public partial class Validator : System.Web.UI.Page
    {
    private readonly string strPicPath = "images\\Validator.jpg";

    protected void Page_Load(object sender, EventArgs e)
    {
    string strOut = GetRandom(10000, 99999);
    Session.Add("randomNum", strOut); //将随机数保存到Session
    OutImages(strOut);
    }

    /// <summary>
    /// 得到一个指定范围的随机数
    /// </summary>
    /// <param name="MinNumeric">随机数的最小选取范围</param>
    /// <param name="MaxNumeric">随机数的最大选取范围</param>
    /// <returns>随机数</returns>
    private string GetRandom(int MinNumeric, int MaxNumeric)
    {
    long liResult = 0; //存放生成的随机数
    Random ro = new Random(); //产生一个随机数实例
    liResult = ro.Next(MinNumeric, MaxNumeric); //产生一个指定范围之间的随机数
    return liResult.ToString(); //返回得到结果
    }

    /// <summary>
    /// 将生成的随机数写入到生成的JPG文件中
    /// </summary>
    /// <param name="strOutput">生成的随机数</param>
    private void OutImages(string strOutput)
    {
    Bitmap bitMaping = new Bitmap(Server.MapPath(strPicPath)); //创建一个位图文件得到它的句柄

    Graphics graphicImage = Graphics.FromImage(bitMaping); //得到一个图形对象

    graphicImage.SmoothingMode = SmoothingMode.HighSpeed;//设置此图形的画笔模式为高速

    graphicImage.DrawString(strOutput, new Font("宋体", 16, FontStyle.Bold), SystemBrushes.WindowText, new Point(0, 0));

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

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

    graphicImage.Dispose();

    bitMaping.Dispose(); //释放资源

    }
    }

    下面是前端页面这里没什么讲的,就只一个页面而已
    /*validator.aspx代码段*/ <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Validator.aspx.cs" Inherits="Validator" %>

    <!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>
    </head>
    <body>
    <form id="frmValidator" runat="server">
    <div>

    </div>
    </form>
    </body>
    </html>


    下面是Ajax的实现端,即前台的验证码实现代码段

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>

    <!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 language="javascript" type="text/javascript">
    // <!CDATA[

    function Button1_onclick() {
    var strInput = document.getElementById("Text1").value;
    var strMsg = "";
    if( 1 == Test.CheckValidator(strInput).value)//调用ajax方法验证数据
    {
    strMsg = "验证码正确";
    }
    else
    {
    strMsg = "验证码错误";
    }
    document.getElementById("Label1").innerHTML = strMsg;

    }

    // ]]>
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
     <input id="Text1" type="text" />
    <asp:Image ID="Image1" runat="server" ImageUrl="Validator.aspx" /><br />
    <input id="Button1" type="button" value="验证" onclick="return Button1_onclick()" />
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>

    </form>
    </body>
    </html>



    这里是后台操作类
    把前面存在Session中的代码与通过Ajax方法传进来的参数进行比较返回比较结果
    再由Label标签打印出结果


    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using AjaxPro;
    public partial class Test : System.Web.UI.Page
    {

    protected void Page_Load(object sender, EventArgs e)
    {
    AjaxPro.Utility.RegisterTypeForAjax(typeof(Test));

    }


    [AjaxPro.AjaxMethod]
    public int CheckValidator(string strInput)
    {
    int iState = 0;
    if (strInput.Equals(Session["randomNum"].ToString()))
    {
    iState = 1;
    }

    return iState;

    }

    }
  • 相关阅读:
    使用PHPExcel 读取 表格数据, 发现中文全变成 FALSE??
    使用百度编辑器--ueditor,后台接收提交编辑的内容,HTML不见了, 赋值不了,赋值之后,html暴露出来了??
    PHP中使用RabiitMQ---各项参数的使用方法
    Java包装类、拆箱和装箱详解
    Eclipse中新建Java工程的三个JRE选项区别
    博客园的代码运行
    35个jQuery小技巧(代码)
    前端最全的 API 集锦
    我的博客开张了
    达梦数据库TPCC测试
  • 原文地址:https://www.cnblogs.com/shinefzh/p/1128883.html
Copyright © 2011-2022 走看看