首页 > 代码库 > 验证码

验证码

技术分享
 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=http://www.mamicode.com/"../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=http://www.mamicode.com/"ValidateImageCode.ashx" id="imgVCode" /><a href=http://www.mamicode.com/"javascript:void(0)" id="kanbuqing">看不清?</a><Br />
展示验证码

 

验证码