标签:des style blog http io ar color sp for
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Blog.UI { using System.Drawing; using Blog.Common; /// <summary> /// Vcode 的摘要说明 /// </summary> public class Vcode : IHttpHandler,System.Web.SessionState.IRequiresSessionState { Random r = new Random(); public void ProcessRequest(HttpContext context) { //准备一张画布 using (Image img = new Bitmap(60, 25)) { //准备一个画家 Graphics g = Graphics.FromImage(img); //背景画白 g.Clear(Color.White); //画边框 g.DrawRectangle(Pens.Red, 0, 0, img.Width - 1, img.Height - 1); //画噪点线 DrawPoints(g, img, 10); //得到验证码字符串 string vcodestr = Getstring(1); //画字符串 g.DrawString(vcodestr, new Font("微软雅黑", 16, FontStyle.Italic | FontStyle.Strikeout), Brushes.Blue, 0, 0); DrawPoints(g, img, 10); //将图片以jpg格式显示在outputstream流 img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); //存进session context.Session[CommonHelper.Vcode] = vcodestr; } } void DrawPoints(Graphics g,Image img,int n) { for (int i = 0; i < n; i++) { int x = r.Next(0, img.Width); int y = r.Next(0, img.Height); g.DrawLine(Pens.Blue, x, y, x + r.Next(2, img.Width), y + r.Next(2, img.Width)); } } string Getstring(int n) { string s = string.Empty; char[] c = new char[] { ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘a‘, ‘b‘, ‘c‘,‘d‘ }; for (int i = 0; i < n; i++) { s+=c[ r.Next(c.Length)]; } return s; } public bool IsReusable { get { return false; } } } }
//加cookie,键值对
HttpCookie cookie = new HttpCookie("name", name);
//expires获取或者设置cookie过期时间,使其变成硬盘cookie
,不加该属性,cookie就是缓存中关闭浏览器就over
cookie.Expires = DateTime.Now.AddMinutes(5);
//将cookie交给浏览器。老中医将病例本交给病人。
Response.Cookies.Add(cookie);
//当跳转之后,也就是再来看病,会带着cookie过来,从
//request.cookies[""] 取出来
session 滑动过期机制20分钟
当session创建时候,实际是系统自动将session的key生成了一个不会重复
的字符串,放进cookie中,发给了浏览器的缓存。这个cookie的key是固定
ASP.NET_SessionId,value就是session的key。可以考虑模拟拿到session
的key研究破解,或者绕过登陆。
session可以放任意类型,cookie只能放文本
session也是键值对,key是一个不重复随机字符串,value里又是键值对存
放用户信息。
session.abandon()//销毁服务器端的session对象(退出)
session.clear()//清空服务器端的session对象里的键值对,session对象没
有从session池里销毁
标签:des style blog http io ar color sp for
原文地址:http://www.cnblogs.com/zhanying/p/4136325.html