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 = ...
随机推荐
- sql server 2008 case when
CASE WHEN的两种格式 1.简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END 2.Case搜索函数 CASE ...
- ionic list item-radio checked
<div class="list"> <label class="item item-radio" ng-repeat="k in ...
- android 多线程断点续传下载
今天跟大家一起分享下Android开发中比较难的一个环节,可能很多人看到这个标题就会感觉头很大,的确如果没有良好的编码能力和逻辑思维,这块是很难搞明白的,前面2次总结中已经为大家分享过有关技术的一些基 ...
- BZOJ1642: [Usaco2007 Nov]Milking Time 挤奶时间
1642: [Usaco2007 Nov]Milking Time 挤奶时间 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 525 Solved: 30 ...
- 在 Windows 下远程桌面连接 Linux - XManager 篇
XManager是一个简单易用的高性能的运行在Windows平台上的X-Server软件,而Gnome和KDE就是X-Client,Linux下的X-Server则为Xorg.它能把远端Unix/Li ...
- Apache安装完服务没有安装的情况
安装完apache之后(不是按照默认路径安装的,例如是 D:\ )右下方哪个小羽毛图标是没有启动的,左键不好使,而且提示“No services installed”,提示服务器没有被安装.解决方法: ...
- HDOJ(HDU) 1976 Software Version(简单判断)
Problem Description 相信大家一定有过在网上下载软件而碰到多个不同版本的情况. 一般来说,软件的版本号由三个部分组成,主版本号(Major Version Number),子版本号( ...
- Hot to get clicked cell column in DevExpress XtraGrid
To accomplish this task, use the GridView.RowCellClickevent. Please note that this event will not fi ...
- nova-network创建初始化网络
nova-network创建初始化网络
- D - Constructing Roads - 2421
题意:有一些村庄需要修一些道路是所有村庄都可以连接,不过有些道路已经修好了,问题最少还需要修建的道路长度是多少. 输入的第一行是一个N代表N个村庄,下面是一个N*N的矩阵,代表着q->j的距离, ...