zoukankan      html  css  js  c++  java
  • 验证码

     1     public class ValidateImageCode:IHttpHandler,System.Web.SessionState.IRequiresSessionState
     2     {
     3 
     4         //一般处理程序中使用Session必须实现IRequiresSessionState接口
     5         public void ProcessRequest(HttpContext context)
     6         {
     7             context.Response.ContentType = "text/html";
     8             Common.ValidateCode validatecode = new Common.ValidateCode();
     9             string validateLength=validatecode.CreateValidateCode(5);
    10             context.Session["validatecode"] = validateLength;
    11             validatecode.CreateValidateGraphic(validateLength,context);
    12         }
    13 
    14         public bool IsReusable
    15         {
    16             get
    17             {
    18                 return false;
    19             }
    20         }
    21     }
    生成验证码
    1 public void ProcessRequest(HttpContext context)
    2     {
    3             context.Response.ContentType = "text/plain";
    4             Common.ValidateCode validatecode = new Common.ValidateCode();
    5             string validateLength=validatecode.CreateValidateCode(5);
    6             validatecode.CreateValidateGraphic(validateLength,context);
    7         }
    验证码
        protected bool CheckValidateCode()
            {
                //定一个变量
                bool isSucess = false;
                //校验Session的时候一定校验是否为空,因为S有过期时间,ToString()的时候会报异常
                if(Session["validatecode"] != null)
                {
                    string txtCode = Request.Form["txtVcode"];
                    string sysCode = Session["validatecode"].ToString();
                    if(sysCode.Equals(txtCode,StringComparison.InvariantCultureIgnoreCase))
                    {
                        isSucess = true;
                        //一定要清空,安全问题。如果不清空 暴力破解 不断猜测随机生成用户名和密码
                        Session["validatecode"] = null;
                    }
                }
                return isSucess;
            }
    验证验证码
     1     <script src="../Js/jquery-1.7.1.js"></script>
     2     <script type ="text/javascript">
     3         $(function () {
     4             $("#kanbuqing").click(function () {
     5                 $("#imgVCode").attr("src","ValidateImageCode.ashx?date=" + new Date().getSeconds());
     6             });
     7         });
     8     </script>
     9 
    10         vcode:<input type="text" name="txtVcode" /><img src="ValidateImageCode.ashx" id="imgVCode" /><a href="javascript:void(0)" id="kanbuqing">看不清?</a><Br />
    展示验证码
  • 相关阅读:
    Windows API
    c# 各类型数据库连接字符串格式
    [C#/C++]C#调用非托管DLL的APIs
    (F#) How to enable F# template working under Visual Studio 2010 Shell.
    ubuntu文件、目录操作基本命令
    javascript curry
    C#中组件与控件的主要区别是什么?
    下拉菜单
    js 尺寸和位置 笔记
    $.each
  • 原文地址:https://www.cnblogs.com/ligtcho/p/6564428.html
Copyright © 2011-2022 走看看