1 using System.Drawing; 2 using System.Web; 3 using System.Web.SessionState; 4 5 /// <summary> 6 /// CaptchaHandler 的摘要說明 7 /// </summary> 8 public class CaptchaHandler : IHttpHandler, IRequiresSessionState //簡記:我需要Session 9 {10 11 public void PRocessRequest(HttpContext context)12 {13 14 // GDI+ 三步 1畫布 2為畫布創建畫筆 3繪制所需素材15 16 var vCode = CaptchaHelper.CreateRandomCode(5); //自己封裝的方法17 18 var buffer = CaptchaHelper.DrawImage(vCode, background: Color.White); //自己封裝的方法19 context.Session["vCode"] = vCode; //vCode:string 類型的驗證碼字符串20 21 context.Response.ContentType = "image/gif";22 context.Response.BinaryWrite(buffer);23 }24 25 public bool IsReusable { get { return false; } }26 }
在一般處理程序中如果要使用Session:
【關鍵】Handler要實現IRequiresSessionState 接口(所在的命名空間using System.Web.SessionState;)
新聞熱點
疑難解答