using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
namespace SmartWaterSys.Web
{
public partial class ValidateCode : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
string checkCode = GetRandomCode();
Session["CheckCode"] = checkCode;
SetPageNoCache();
CreateImage(checkCode);
} /// <summary>
/// 设置页面不被缓存
/// </summary>
private void SetPageNoCache()
{
Response.Buffer = true;
Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-);
Response.Expires = ;
Response.CacheControl = "no-cache";
Response.AppendHeader("Pragma", "No-Cache");
} private string CreateRandomCode(int codeCount)
{
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";
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -; Random rand = new Random();
for (int i = ; i < codeCount; i++)
{
if (temp != -)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next();
if (temp == t)
{
return CreateRandomCode(codeCount);//性能问题
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
private string GetRandomCode(int CodeCount)
{
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";
string[] allCharArray = allChar.Split(',');
string RandomCode = "";
int temp = -; Random rand = new Random();
for (int i = ; i < CodeCount; i++)
{
if (temp != -)
{
rand = new Random(temp * i * ((int)DateTime.Now.Ticks));
} int t = rand.Next(); while (temp == t)
{
t = rand.Next();
} temp = t;
RandomCode += allCharArray[t];
} return RandomCode;
}
private void CreateImage(string checkCode)
{
int iwidth = (int)(checkCode.Length * );
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, );
Graphics g = Graphics.FromImage(image);
Font f = new System.Drawing.Font("Arial ", );//, System.Drawing.FontStyle.Bold);
Brush b = new System.Drawing.SolidBrush(Color.Black);
Brush r = new System.Drawing.SolidBrush(Color.FromArgb(, , )); //g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
// g.Clear(Color.AliceBlue);//背景色
g.Clear(System.Drawing.ColorTranslator.FromHtml("#99C1CB"));//背景色 char[] ch = checkCode.ToCharArray();
for (int i = ; i < ch.Length; i++)
{
if (ch[i] >= '' && ch[i] <= '')
{
//数字用红色显示
g.DrawString(ch[i].ToString(), f, r, + (i * ), );
}
else
{ //字母用黑色显示
g.DrawString(ch[i].ToString(), f, b, + (i * ), );
}
} //for循环用来生成一些随机的水平线
// Pen blackPen = new Pen(Color.Black, 0);
// Random rand = new Random();
// for (int i=0;i<5;i++)
// {
// int y = rand.Next(image.Height);
// g.DrawLine(blackPen,0,y,image.Width,y);
// } System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
//history back 不重复
Response.Cache.SetNoStore();//这一句
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
}
}
百度了一下,应该是为了解决获取验证码前后一致的问题,因为缓存会使数据保存,而验证码恰好不需要保存而是需要更新,所以取消缓存。
因为你每次动态生成的验证码的文件名都是一样的,比如是: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. MYSQL主从同步测试

    参考: http://www.cnblogs.com/zgx/archive/2011/09/13/2174823.html 注意选建同步用户,其它的都按步就搬. 还有,不要让IPTABLES坏事,开 ...

  2. android:ListView的局部刷新

    1.简介 对于android中的ListView刷新机制,大多数的程序员都是很熟悉的,修改或者添加adapter中的数据源之后,然后调用notifyDataSetChanged()刷新ListView ...

  3. MVC加载view的方式

    主要有 Html.ActionLink Html.RenderPartial Html.RenderAction Html.Partial Ajax.ActionLink load 浏览器对象模型 ( ...

  4. ASP.NET 之深入浅出Session和Cookie

    在做人事档案管理系统中,对于Session和Cookie的使用后理解更加深刻了,下面对本知识点总结学习. Session是什么? 简单来说就是服务器给客户端的一个编号.当一台WWW服务器运行时,可能有 ...

  5. 标准的数据获取 -ios

    #define kBgQueue dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) #define kLatestKivaL ...

  6. Tiny语言执行环境TM机源码

    TM机就是TINY语言编译器编译之后的汇编代码的执行环境.TM机的主要功能是将TM的汇编代码读入和执行,它具有一般计算机类似的精简指令级RISC.TM汇编语言和一般的Intel汇编语言差点儿相同,包含 ...

  7. Lecture Halls

    Lecture Halls (会议安排)   时间限制(普通/Java):1000MS/10000MS     运行内存限制:65536KByte 总提交: 38            测试通过: 2 ...

  8. Computer Vision and Machine Learning Competitions

    一.ImageNet Object Detection, Object Classification+Localization 二.COCO Image Captioning 三.LFW Face R ...

  9. 查看linux版本号的几种方法

    (1)lsb_release 命令查看,FSG(Free Standards Group)组织开发的LSB (Linux Standard Base)标准的一个命令,用来查看linux兼容性的发行版信 ...

  10. 异步套接字编程之select模型

      █ 选择(select)模型是Winsock中最常见的 I/O模型.核心便是利用 select 函数,实现对 I/O的管理!利用 select 函数来判断某Socket上是否有数据可读,或者能否向 ...