• 验证码的实现


    JS 实现随机验证码功能
    1、验证码 验证是网页常出现的一个验证点,所谓验证码类型有很多,下面代码只是实现一个简单的验证功能。
    <div>
    <input type = "text" id = "input" value="" />
    <input type = "button" id="code" onclick="createCode()"/>
    <input type = "button" value = "验证" onclick = "validate()"/>
    </div>
    2、随便加点样式
     
    #code{
    font-family:Arial;
    font-style:italic;
    font-weight:bold;
    border:0;
    letter-spacing:2px;
    color:blue;

    }

     
    3.JS部分,里面我已经加了详细的备注

    //设置一个全局的变量,便于保存验证码
    var code;

    function createCode(){

    //首先默认code为空字符串
    code = '';

    //设置长度,这里看需求,我这里设置了4
    var codeLength = 4;

    var codeV = document.getElementById('code');

    //设置随机字符
    var random = new Array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R', 'S','T','U','V','W','X','Y','Z');

    //循环codeLength 我设置的4就是循环4次
    for(var i = 0; i < codeLength; i++){

    //设置随机数范围,这设置为0 ~ 36
    var index = Math.floor(Math.random()*36);

    //字符串拼接 将每次随机的字符 进行拼接
    code += random[index];

    }

    //将拼接好的字符串赋值给展示的Value
    codeV.value = code;

    }

    //下面就是判断是否== 的代码,无需解释
    function validate(){

    var oValue = document.getElementById('input').value.toUpperCase();

    if(oValue ==0){

    alert('请输入验证码');

    }else if(oValue != code){

    alert('验证码不正确,请重新输入');

    oValue = ' ';

    createCode();

    }else{

    window.open('http://www.baidu.com','_self');

    }

    }

    //设置此处的原因是每次进入界面展示一个随机的验证码,不设置则为空
    window.onload = function (){

    createCode();

    }

    ----------------------------------------------------------------------------------

    第三方验证控件

    一般处理程序
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.SessionState;

    namespace UI
    {
    /// <summary>
    /// ValidateCode1 的摘要说明
    /// </summary>
    public class ValidateCode1 : IHttpHandler, IRequiresSessionState
    {
    /// <summary>
    /// 验证码类型(0-字母数字混合,1-数字,2-字母)
    /// </summary>
    private string validateCodeType = "0";
    /// <summary>
    /// 验证码字符个数
    /// </summary>
    private int validateCodeCount = 4;
    /// <summary>
    /// 验证码的字符集,去掉了一些容易混淆的字符
    /// </summary>
    char[] character = { '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
    'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };

    public void ProcessRequest(HttpContext context)
    {
    context.Response.BufferOutput = true;
    context.Response.Cache.SetExpires(DateTime.Now.AddMilliseconds(-1));
    context.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
    context.Response.AppendHeader("Pragma", "No-Cache");
    //获取设置参数
    if (!string.IsNullOrEmpty(context.Request.QueryString["validateCodeType"]))
    {
    validateCodeType = context.Request.QueryString["validateCodeType"];
    }
    if (!string.IsNullOrEmpty(context.Request.QueryString["validateCodeCount"]))
    {
    int.TryParse(context.Request.QueryString["validateCodeCount"], out validateCodeCount);
    }
    //生成验证码
    this.CreateCheckCodeImage(GenerateCheckCode(context), context);
    }

    private string GenerateCheckCode(HttpContext context)
    {
    char code;
    string checkCode = String.Empty;
    System.Random random = new Random();

    for (int i = 0; i < validateCodeCount; i++)
    {
    code = character[random.Next(character.Length)];

    // 要求全为数字或字母
    if (validateCodeType == "1")
    {
    if ((int)code < 48 || (int)code > 57)
    {
    i--;
    continue;
    }
    }
    else if (validateCodeType == "2")
    {
    if ((int)code < 65 || (int)code > 90)
    {
    i--;
    continue;
    }
    }
    checkCode += code;
    }

    context.Response.Cookies.Add(new System.Web.HttpCookie("CheckCode", checkCode));
    context.Session.Add("CheckCode",checkCode);
    return checkCode;
    }

    private void CreateCheckCodeImage(string checkCode, HttpContext context)
    {
    if (checkCode == null || checkCode.Trim() == String.Empty)
    return;

    System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling

    ((checkCode.Length * 15.0 + 40)), 23);
    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);

    try
    {
    //生成随机生成器
    Random random = new Random();

    //清空图片背景色
    g.Clear(System.Drawing.Color.White);

    //画图片的背景噪音线
    for (int i = 0; i < 25; i++)
    {
    int x1 = random.Next(image.Width);
    int x2 = random.Next(image.Width);
    int y1 = random.Next(image.Height);
    int y2 = random.Next(image.Height);

    g.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Silver), x1, y1, x2,

    y2);
    }

    System.Drawing.Font font = new System.Drawing.Font("Arial", 14,

    (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
    System.Drawing.Drawing2D.LinearGradientBrush brush = new

    System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.Rectangle(0, 0,

    image.Width, image.Height), System.Drawing.Color.Blue, System.Drawing.Color.DarkRed, 1.2f,

    true);

    int cySpace = 16;
    for (int i = 0; i < validateCodeCount; i++)
    {
    g.DrawString(checkCode.Substring(i, 1), font, brush, (i + 1) * cySpace, 1);
    }

    //画图片的前景噪音点
    for (int i = 0; i < 100; i++)
    {
    int x = random.Next(image.Width);
    int y = random.Next(image.Height);

    image.SetPixel(x, y, System.Drawing.Color.FromArgb(random.Next()));
    }

    //画图片的边框线
    g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Silver), 0, 0,

    image.Width - 1, image.Height - 1);

    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    context.Response.ClearContent();
    context.Response.ContentType = "image/Gif";
    context.Response.BinaryWrite(ms.ToArray());
    }
    finally
    {
    g.Dispose();
    image.Dispose();
    }
    }

    public bool IsReusable
    {
    get
    {
    return false;
    }
    }
    }
    }


    显示:
    <p>验证码:<input type="text" name="Yan" /><img src="/Content/ValidateCode.ashx" onclick="this.src = '/Content/ValidateCode.ashx?id='+new Date()"></p>

    验证:Yan.ToUpper() == Session["CheckCode"].ToString()

  • 相关阅读:
    总结的git操作命令小抄集
    两种方式实现压缩文件或文件夹
    eclipse 中执行 main 函数如何添加参数
    alert()、confirm()和prompt()的区别与用法
    阿里某安全工程师写的明星代码
    在 Linux 环境下报错 java.lang.reflect.InvocationTargetException
    MyBatis学习-SQL 符号篇
    初识IP基础分类、CIDR
    Snort
    Dshell----开源攻击分析框架
  • 原文地址:https://www.cnblogs.com/LiChen19951127/p/9672435.html
Copyright © 2020-2023  润新知