验证码纯数字(四位生成)

Page_Load(object sender,EventArgs e){

try{

String checkCode= GetRandomCode(4);//调用方法

Session[“checkCode”]=checkCode;//存入SESSION。

SetPageNoCache();//禁止页面缓存

CreateImage(checkCode);//生成验证码图片

}catch{}

}

//生成一定长度的验证码

private String GetRandomCode(int length){

int rand;

char code;

string randomcode=String.Empty;

System.Random random=new Random();// 伪随机数生成器

for(int i=0;i<length;i++){

rand=random.Next();//返回一个非负的伪随机数

code=(char)(‘0’+(char)(rand%10)); // 一个数模十 余数 肯定 是0~9.前面连个0最终的Code值将是 0×;

randomcode+=code.ToString();//累计连接 CODE 字符

}

return randomcode.ToLower();//小写,把字符转换成小写字母,非字母字符不做出处理

}

//为了保证每次显示的生产图片和内存中实际的验证码一致,要禁止页面缓存

private void SetPageNoCache(){

Response.Buffer=true;// 是否缓存输出并在处理完响应之后发生它。

Response.ExpireAbsolute=System.DateTime.Now.AddSeconds(-1);//设置过期时间的绝对值为当前时间-1,即出来后就过期了

这是因为默认情况下,浏览器将页面先载入缓存,然后才显示出来.通过上述设置,使浏览器对页面不进行缓存,所以当用户点"后退"后,由于页面没有缓存,所以浏览器只能再次通过向服务器发出HTTP请求来得到页面,这样就实现了限制了用户对同一网页进行多次操作。

Response.Expires=0;//

般情况下,当用户请求WEB服务器时,服务器把请求回复给客户端。在客户端,浏览器把缓存作为一种加快速度的策略,就是当请求时先检查缓存的情况,如果有就直接调缓存了,而不请求服务器了。 
在WEB程序设计中,有时为了防止重复的提交或者严格的次序等,必须让用户的数据立即在使用后就过期,当用户后退时就显示过期而不能继续使用。也算是为安全考虑的吧。 
一般,response.expires写在页面的最上端,后面跟的是一个时间,就是过期的时间,0表示立即过期。

-1告诉浏览器不缓存本页

Response.CacheControl = "no-cache"

//

设置不缓存在临时文件中.本句是指,上网时看到的图片通常在internet的临时文件都能够找到对应的文件,如果加上这一句就找不到了

Response.AppendHeader("Pragma", "No-Cache");

//是不要网页存于缓存之中。

相当于<meta http-equiv="pragma" content="no-cache">
这句的意思是向返回的头信息里添加一个KEY
 
 
//////////////////////

response.Buffer=true的意思就是指明输出页面是否被缓冲,当属性值为True时,服务器将不会向客户端发送任何信息,直到所有程序执行完或者遇到
<% Response.Flush %>或<% Response.End %>
语句,才会释放缓冲区的信息。

Expires 属性
Expires 属性指定了在浏览器上缓冲存储的页距过期还有多少时间。如果用户在某个页过期之前又回到此页,就会显示缓冲区中的版本

语法
Response.Expires [= number]

参数
number
距过期还有多少分钟。将此参数设置为 0 可使缓存的页立即过期。
注释
若此属性在一页上设置了多次,则使用最短的时间。

应用于
Response 对象

Response.expires=0也表示立即过期,但如果client和server不在一个时区或者client的时间早于server上的时间,则不能立即过期。所以用负数或者用Response.ExpiresAbsolute=now()-1来表示立即过期,response.expires=1表示在1分钟后过期。

asp中Response.ExpiresAbsolute=now()-1

在asp页面中,通常会有这样的设置。

Response.ExpiresAbsolute=now()-1‘设置过期时间的绝对值为当前时间-1,即出来后就过期了

这是因为默认情况下,浏览器将页面先载入缓存,然后才显示出来.通过上述设置,使浏览器对页面不进行缓存,所以当用户点"后退"后,由于页面没有缓存,所以浏览器只能再次通过向服务器发出HTTP请求来得到页面,这样就实现了限制了用户对同一网页进行多次操作。

asp中Response.Expires = -1

一般情况下,当用户请求WEB服务器时,服务器把请求回复给客户端。在客户端,浏览器把缓存作为一种加快速度的策略,就是当请求时先检查缓存的情况,如果有就直接调缓存了,而不请求服务器了。
在WEB程序设计中,有时为了防止重复的提交或者严格的次序等,必须让用户的数据立即在使用后就过期,当用户后退时就显示过期而不能继续使用。也算是为安全考虑的吧。
一般,response.expires写在页面的最上端,后面跟的是一个时间,就是过期的时间,0表示立即过期。

asp中对浏览器缓存的设置

Response.CacheControl = "no-cache"

设置不缓存在临时文件中.本句是指,上网时看到的图片通常在internet的

//////////////////

}

///  <summary>
        ///  创建随机码图片
        ///  </summary>
        ///  <param  name="randomcode">随机码</param>
        private void CreateImage(string randomcode)
        {
            int randAngle = 45; //随机转动角度
            int mapwidth = (int)(randomcode.Length * 23);
            Bitmap map = new Bitmap(mapwidth, 42);//创建图片背景
            Graphics graph = Graphics.FromImage(map);
            graph.Clear(Color.LightSkyBlue);//清除画面,填充背景
            // graph.DrawRectangle(new Pen(Color.Black, 0), 0, 0, map.Width - 1, map.Height - 1);//画一个边框
            //graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//模式

            Random rand = new Random();

            //背景噪点生成
            Pen blackPen = new Pen(Color.LightGray, 0);
            for (int i = 0; i < 50; i++)
            {
                int x = rand.Next(0, map.Width);
                int y = rand.Next(0, map.Height);
                graph.DrawRectangle(blackPen, x, y, 1, 1);
            }

            //验证码旋转,防止机器识别
            char[] chars = randomcode.ToCharArray();//拆散字符串成单字符数组

            //文字距中
            StringFormat format = new StringFormat(StringFormatFlags.NoClip);
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;

            //定义颜色
            Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
            //定义字体
            string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };

            for (int i = 0; i < chars.Length; i++)
            {
                int cindex = rand.Next(7);
                int findex = rand.Next(5);

                Font f = new System.Drawing.Font(font[findex], 23, System.Drawing.FontStyle.Bold);//字体样式(参数2为字体大小)
                Brush b = new System.Drawing.SolidBrush(c[cindex]);

                Point dot = new Point(16, 16);
                //graph.DrawString(dot.X.ToString(),fontstyle,new SolidBrush(Color.Black),10,150);//测试X坐标显示间距的
                float angle = rand.Next(-randAngle, randAngle);//转动的度数

                graph.TranslateTransform(dot.X, dot.Y);//移动光标到指定位置
                graph.RotateTransform(angle);
                graph.DrawString(chars[i].ToString(), f, b, 1, 1, format);
                //graph.DrawString(chars[i].ToString(),fontstyle,new SolidBrush(Color.Blue),1,1,format);
                graph.RotateTransform(-angle);//转回去
                graph.TranslateTransform(2, -dot.Y);//移动光标到指定位置
            }
            //graph.DrawString(randomcode,fontstyle,new SolidBrush(Color.Blue),2,2); //标准随机码

            //生成图片
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            map.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            Response.ClearContent();
            Response.ContentType = "image/gif";
            Response.BinaryWrite(ms.ToArray());
            graph.Dispose();
            map.Dispose();
        }

        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }

原文地址:https://www.cnblogs.com/mahaisong/p/1969629.html