1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.Web.UI;
  5. using System.Web.UI.WebControls;
  6. using System.Drawing;
  7. namespace SmartWaterSys.Web
  8. {
  9. public partial class ValidateCode : System.Web.UI.Page
  10. {
  11. private void Page_Load(object sender, System.EventArgs e)
  12. {
  13. string checkCode = GetRandomCode();
  14. Session["CheckCode"] = checkCode;
  15. SetPageNoCache();
  16. CreateImage(checkCode);
  17. }
  18.  
  19. /// <summary>
  20. /// 设置页面不被缓存
  21. /// </summary>
  22. private void SetPageNoCache()
  23. {
  24. Response.Buffer = true;
  25. Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-);
  26. Response.Expires = ;
  27. Response.CacheControl = "no-cache";
  28. Response.AppendHeader("Pragma", "No-Cache");
  29. }
  30.  
  31. private string CreateRandomCode(int codeCount)
  32. {
  33. string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,M,N,P,Q,R,S,T,U,W,X,Y,Z";
  34. string[] allCharArray = allChar.Split(',');
  35. string randomCode = "";
  36. int temp = -;
  37.  
  38. Random rand = new Random();
  39. for (int i = ; i < codeCount; i++)
  40. {
  41. if (temp != -)
  42. {
  43. rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
  44. }
  45. int t = rand.Next();
  46. if (temp == t)
  47. {
  48. return CreateRandomCode(codeCount);//性能问题
  49. }
  50. temp = t;
  51. randomCode += allCharArray[t];
  52. }
  53. return randomCode;
  54. }
  55. private string GetRandomCode(int CodeCount)
  56. {
  57. string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,M,N,P,Q,R,S,T,U,W,X,Y,Z";
  58. string[] allCharArray = allChar.Split(',');
  59. string RandomCode = "";
  60. int temp = -;
  61.  
  62. Random rand = new Random();
  63. for (int i = ; i < CodeCount; i++)
  64. {
  65. if (temp != -)
  66. {
  67. rand = new Random(temp * i * ((int)DateTime.Now.Ticks));
  68. }
  69.  
  70. int t = rand.Next();
  71.  
  72. while (temp == t)
  73. {
  74. t = rand.Next();
  75. }
  76.  
  77. temp = t;
  78. RandomCode += allCharArray[t];
  79. }
  80.  
  81. return RandomCode;
  82. }
  83. private void CreateImage(string checkCode)
  84. {
  85. int iwidth = (int)(checkCode.Length * );
  86. System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, );
  87. Graphics g = Graphics.FromImage(image);
  88. Font f = new System.Drawing.Font("Arial ", );//, System.Drawing.FontStyle.Bold);
  89. Brush b = new System.Drawing.SolidBrush(Color.Black);
  90. Brush r = new System.Drawing.SolidBrush(Color.FromArgb(, , ));
  91.  
  92. //g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
  93. // g.Clear(Color.AliceBlue);//背景色
  94. g.Clear(System.Drawing.ColorTranslator.FromHtml("#99C1CB"));//背景色
  95.  
  96. char[] ch = checkCode.ToCharArray();
  97. for (int i = ; i < ch.Length; i++)
  98. {
  99. if (ch[i] >= '' && ch[i] <= '')
  100. {
  101. //数字用红色显示
  102. g.DrawString(ch[i].ToString(), f, r, + (i * ), );
  103. }
  104. else
  105. { //字母用黑色显示
  106. g.DrawString(ch[i].ToString(), f, b, + (i * ), );
  107. }
  108. }
  109.  
  110. //for循环用来生成一些随机的水平线
  111. // Pen blackPen = new Pen(Color.Black, 0);
  112. // Random rand = new Random();
  113. // for (int i=0;i<5;i++)
  114. // {
  115. // int y = rand.Next(image.Height);
  116. // g.DrawLine(blackPen,0,y,image.Width,y);
  117. // }
  118.  
  119. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  120. image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  121. //history back 不重复
  122. Response.Cache.SetNoStore();//这一句
  123. Response.ClearContent();
  124. Response.ContentType = "image/Jpeg";
  125. Response.BinaryWrite(ms.ToArray());
  126. g.Dispose();
  127. image.Dispose();
  128. }
  129. }
  130. }
百度了一下,应该是为了解决获取验证码前后一致的问题,因为缓存会使数据保存,而验证码恰好不需要保存而是需要更新,所以取消缓存。
  1. 因为你每次动态生成的验证码的文件名都是一样的,比如是:img.jpg 那么系统就缓存了,下次再调用这个页面的时候ie会认为存在这个图片不会更新。导致验证码不对。

ValidateCode.cs验证码时设置缓存的使用的更多相关文章

  1. Django (八) 中间件&验证码&富文本&缓存

    中间件&验证码&富文本&缓存 1. 中间件&AOP   中间件:是一个轻量级的,底层的插件,可以介入Django的请求和响应过程(面向切面编程) ​ 中间件的本质就是一 ...

  2. Tp验证码:$Verify = new \Think\Verify(); $Verify->entry(n);【参数n,页面有多个验证码时用】

    一.验证码参数:(中文字符集和英文字符集在父类里面都可以取到,可修改) //1.生成验证码 $Verify = new \Think\Verify(); $Verify->entry(n);[参 ...

  3. Ehcache(04)——设置缓存的大小

    http://haohaoxuexi.iteye.com/blog/2116749 设置缓存的大小 目录 1     CacheManager级别 2     Cache级别 3     大小衡量 4 ...

  4. 详解浏览器缓存机制与Apache设置缓存

    一.详解浏览器缓存机制 对于,如何说明缓存机制,在网络上找到了两张图,个人认为思路是比较清晰的.总结时,上图. 这里需要注意的有两点: 1.Last-Modified.Etag是响应头里的数据 2.I ...

  5. c# .net core 设置缓存

    1.开启ResponseCaching的缓存(ResponseCaching相当于老版本的OutPutCache): 在Startup.cs文件中设置: public void ConfigureSe ...

  6. mysql系列三、mysql开启缓存、设置缓存大小、缓存过期机制

    一.开启缓存 mysql 开启查询缓存可以有两种方法来开启一种是使用set命令来进行开启,另一种是直接修改my.ini文件来直接设置都是非常的简单的哦. 开启缓存,设置缓存大小,具体实施如下: 1.修 ...

  7. 理解Solr缓存及如何设置缓存大小

    文献地址:http://wangdg.com/understanding-and-tuning-solr-cache/ 理解Solr缓存及如何设置缓存大小 为了得到最好的检索性能,Solr会在内存中缓 ...

  8. C#中缓存的简单方法及使用Sql设置缓存依赖项

    概述 使用Cache高速缓存可以提高数据的读取速度,减少服务器与客户端之间的数据交互.因为Cache一经创建就会占用服务器上的资源,所以Cache并不是越多越好,一般用于数据较固定,使用较频繁的地方. ...

  9. 使用Volley缓存图片时,缓存无效的原因。

    使用Volley的ImageLoader异步获取并缓存图片时,发现有的网络图片已经缓存了,可是断网后却读不出来. ImageLoader的用法: RequestQueue requestQueue = ...

随机推荐

  1. Java中new关键字和newInstance方法的区别

    在初始化一个类,生成一个实例的时候,newInstance()方法和new关键字除了一个是方法一个是关键字外,最主要的区别是创建对象的方式不同.newInstance()使用类加载机制,new是创建一 ...

  2. hdu 5125 magic balls

    题意:求a数组的LIS,但是加了一个条件,为了LIS最大 b[i] a[i]可以交换.最多交换m次: 思路:我们令dp[i][j][l]表示i在最长上升子序列中,已经损失j点能量,第i个人转换了ai和 ...

  3. armv8(aarch64)linux内核中flush_dcache_all函数详细分析

    /* *  __flush_dcache_all() *  Flush the wholeD-cache. * Corrupted registers: x0-x7, x9-x11 */ ENTRY( ...

  4. android开发--翻转闹铃(从制作到打包)

    (转载请声明,文章原作地址http://blog.csdn.net/buptgshengod) 最近在家放假,一直想做一个手机应用,于是就自己动手做起来了.想到一个注意就是当闹铃响的时候翻转闹铃,声音 ...

  5. Delphi实现AnsiString与WideString的转换函数 转

    Delphi实现AnsiString与WideString的转换函数 分类: Delphi2013-01-26 16:23 460人阅读 评论(0) 收藏 举报 [delphi] view plain ...

  6. Java中String的哈希值计算

    下面都是从String类的源码中粘贴出来的 private int hash; // Default to 0 public int hashCode() { int h = hash; if (h ...

  7. Aggregation Models

    这是Coursera上<机器学习技法>的课程笔记. Aggregation models: mix or combine hypotheses for better performance ...

  8. java笔记11之二维数组

    格式1: 二维数组:就是元素为一维数组的一个数组 数据类型[][] 数组名 = new 数组类型[m][n] 其中m为行 n为列 注意: A:以下格式也可以表示二维数组            a:数据 ...

  9. 通过分析 JDK 源代码研究 Hash 存储机制--转载

    通过 HashMap.HashSet 的源代码分析其 Hash 存储机制 集合和引用 就像引用类型的数组一样,当我们把 Java 对象放入数组之时,并不是真正的把 Java 对象放入数组中,只是把对象 ...

  10. -bash: ulimit: open files: cannot modify limit: Operation not permitted

    普通用户登录系统报错,提示: -bash: ulimit: open files: cannot modify limit: Operation not permitted. 处理方法: #vi /e ...