ValidateCode.cs验证码时设置缓存的使用
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验证码时设置缓存的使用的更多相关文章
- Django (八) 中间件&验证码&富文本&缓存
中间件&验证码&富文本&缓存 1. 中间件&AOP 中间件:是一个轻量级的,底层的插件,可以介入Django的请求和响应过程(面向切面编程) 中间件的本质就是一 ...
- Tp验证码:$Verify = new \Think\Verify(); $Verify->entry(n);【参数n,页面有多个验证码时用】
一.验证码参数:(中文字符集和英文字符集在父类里面都可以取到,可修改) //1.生成验证码 $Verify = new \Think\Verify(); $Verify->entry(n);[参 ...
- Ehcache(04)——设置缓存的大小
http://haohaoxuexi.iteye.com/blog/2116749 设置缓存的大小 目录 1 CacheManager级别 2 Cache级别 3 大小衡量 4 ...
- 详解浏览器缓存机制与Apache设置缓存
一.详解浏览器缓存机制 对于,如何说明缓存机制,在网络上找到了两张图,个人认为思路是比较清晰的.总结时,上图. 这里需要注意的有两点: 1.Last-Modified.Etag是响应头里的数据 2.I ...
- c# .net core 设置缓存
1.开启ResponseCaching的缓存(ResponseCaching相当于老版本的OutPutCache): 在Startup.cs文件中设置: public void ConfigureSe ...
- mysql系列三、mysql开启缓存、设置缓存大小、缓存过期机制
一.开启缓存 mysql 开启查询缓存可以有两种方法来开启一种是使用set命令来进行开启,另一种是直接修改my.ini文件来直接设置都是非常的简单的哦. 开启缓存,设置缓存大小,具体实施如下: 1.修 ...
- 理解Solr缓存及如何设置缓存大小
文献地址:http://wangdg.com/understanding-and-tuning-solr-cache/ 理解Solr缓存及如何设置缓存大小 为了得到最好的检索性能,Solr会在内存中缓 ...
- C#中缓存的简单方法及使用Sql设置缓存依赖项
概述 使用Cache高速缓存可以提高数据的读取速度,减少服务器与客户端之间的数据交互.因为Cache一经创建就会占用服务器上的资源,所以Cache并不是越多越好,一般用于数据较固定,使用较频繁的地方. ...
- 使用Volley缓存图片时,缓存无效的原因。
使用Volley的ImageLoader异步获取并缓存图片时,发现有的网络图片已经缓存了,可是断网后却读不出来. ImageLoader的用法: RequestQueue requestQueue = ...
随机推荐
- 常用的JQuery UI框架
http://www.ligerui.com/ http://www.jeasyui.com/index.php http://www.jqwidgets.com/
- SANS top 20
What Are the Controls?The detailed Consensus Audit Guidelines are posted at http://www.sans.org/cag/ ...
- h.264参考图像列表、解码图像缓存
1.参考图像列表(reference picture list) 一般来说,h.264会把需要编码的图像分为三种类型:I.P.B,其中的B.P类型的图像由于采用了帧间编码的这种编码方式,而帧间编码又是 ...
- Xamarin.Forms本地化多语言
Forms 右键新建文件夹 Localization 新建文件TranslateExtension.cs namespace Localization { /// <summary> // ...
- LTE发射机ACLR性能的测量技术
现代无线服务提供商正致力于不断扩大带宽,为更多用户提供互联网协议(IP)服务.长期演进技术(LTE)是对当前部署的3GPP 网络进行增强并创造更多更重要应用的新一代蜂窝技术.LTE 的体系结构复杂同时 ...
- UOJ #78 二分图最大匹配
#78. 二分图最大匹配 从前一个和谐的班级,有 nl 个是男生,有 nr 个是女生.编号分别为 1,…,nl 和 1,…,nr. 有若干个这样的条件:第 v 个男生和第 u 个女生愿意结为配偶. 请 ...
- 定义一个栈的数据结构,要求实现一个min函数,每次能够得到栈的最小值,并且要求Min的时间复杂度为O(1)
具体实现代码如下: stack.h内容如下: #ifndef _STACK_H_ #define _STACK_H_ #define NUM 256 typedef struct _tagStack ...
- D - Mayor's posters - 2528(区间覆盖)
题意:贴海报 有一面很长的墙,大概有10000000 这么长,现有有一些海报会贴在墙上,当然贴海报的顺序是有先后的,问你当最后一张海报也贴上的时候能不能求出来在这面墙上能看到多少张不同的海报? 分析: ...
- DateGradeView分页绑定
<form method="post" id="nform" runat="server"> < ...
- 【深入了解cocos2d-x 3.x】定时器(scheduler)的使用和原理探究(3)
上篇文章分析到了定时器的定义.这篇的重点就是定时器是怎样执行起来的. 1.从main中寻找定时器的回调 讲定时器的执行,就不得不触及到cocos2dx的main函数了,由于定时器是主线程上执行的.并非 ...