zoukankan      html  css  js  c++  java
  • c#验证码的生成

     一:方法一  

    1.新建一个一般处理程序文件 2 /// <summary> 3 /// 验证码 4 /// </summary> 5 public class ValidateImage : IHttpHandler, IRequiresSessionState 6 { 7 int intLength = 4; //长度 8 string strIdentify = "IdentifyCode"; //随机字串存储键值,以便存储到Session中 9 10 public ValidateImage() 11 { 12 } 13 14 /// <summary> 15 /// 生成验证图片核心代码 16 /// </summary> 17 /// <param name="hc"></param> 18 public void ProcessRequest(HttpContext hc) 19 { 20 //设置输出流图片格式 21 if (hc.Session["valcode"]!=null){ 22 hc.Session["valcode"] = null; 23 } 24 hc.Response.ContentType = "image/gif"; 25 Bitmap b = new Bitmap(70, 30); 26 Graphics g = Graphics.FromImage(b); 27 g.FillRectangle(new SolidBrush(Color.YellowGreen), 0, 0, 70, 30); 28 Font font = new Font(FontFamily.GenericSerif, 24, FontStyle.Bold, GraphicsUnit.Pixel); 29 Random r = new Random(); 30 31 //合法随机显示字符列表 32 string strLetters = "1234567890";// "abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"; 33 StringBuilder s = new StringBuilder(); 34 35 //将随机生成的字符串绘制到图片上 36 for (int i = 0; i < intLength; i++) 37 { 38 s.Append(strLetters.Substring(r.Next(0, strLetters.Length - 1), 1)); 39 g.DrawString(s[s.Length - 1].ToString(), font, new SolidBrush(Color.Blue), i * 15, r.Next(0, 8)); 40 } 41 42 ////生成干扰线条 43 Pen pen = new Pen(new SolidBrush(Color.Blue), 2); 44 for (int i = 0; i < 5; i++) 45 { 46 g.DrawLine(pen, new Point(r.Next(0, 100), r.Next(0, 59)), new Point(r.Next(0, 199), r.Next(0, 59))); 47 } 48 b.Save(hc.Response.OutputStream, ImageFormat.Gif); 49 // hc.Session[strIdentify] = s.ToString(); //先保存在Session中,验证与用户输入是否一致 50 Chain.Common.ValCodeModel CMVc = new Chain.Common.ValCodeModel(); 51 CMVc.valCode = s.ToString(); 52 CMVc.Failure = 3 * 60; 53 CMVc.CreateDate = DateTime.Now; 54 hc.Session["valcode"] = CMVc; 55 //hc.Response.SetCookie(new HttpCookie("identifyCode", s.ToString())); 56 hc.Response.End(); 57 58 } 59 60 /// <summary> 61 /// 表示此类实例是否可以被多个请求共用(重用可以提高性能) 62 /// </summary> 63 public bool IsReusable 64 { 65 get 66 { 67 return true; 68 } 69 } 70 }
    2.页面显示验证码处理:
    <div class="check">
                                <a href="javascript:void(0);" onclick="javascript:Login_ChangeValImg();">
                                    <img id="Login_ValImg" src="" style="height: 30px;  90px; border: 0" alt=""
                                        title="看不清?换一个" align="top" /></a>
                            </div>

    3.js页面处理

     1 $(document).ready(function () {
     2     Login_ChangeValImg();
     3 });
     4 function Login_ChangeValImg() {
     5     /*自动刷新验证码*/
     6     ChangeValImg();
     7     setTimeout("Login_ChangeValImg()", 10 * 60 * 1000);
     8 }
     9 
    10 function ChangeValImg() {
    11     $("#Login_ValImg").attr("src", "Service/ValidateImage.ashx?" + GetGuid());
    12     $("#txtValCode").val("");
    13 }






  • 相关阅读:
    对于java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I错误解决
    attempted to assign id from null one-to-one
    The class has no identifier property
    javax.servlet.ServletException: com.microsoft.jdbc.base.BaseDatabaseMetaData.supportsGetGeneratedKeys()Z
    final和static
    hibernate事务
    log4j:WARN Please initialize the log4j system properly.解决
    用最有效率的方法算出2乘以8等於几
    char型变量中能存贮一个中文汉字
    基本数据类型范围
  • 原文地址:https://www.cnblogs.com/wwym/p/5574766.html
Copyright © 2011-2022 走看看