驗證碼是一個圖片是動態生成的,一般的驗證碼保存在服務器中。
要在一般處理程序中使用session必須實現System.Web.SessionState.IRequiresSessionState 接口不然會出現找不到session的錯誤。
public void PRocessRequest (HttpContext context) {
context.Response.ContentType = "image/JPEG";//這里要改正格式以前為(text/plan)
//創建一個位圖確定圖片的大小(圖片的大小)
using (System.Drawing.Bitmap bitmip=new System.Drawing.Bitmap(100,50))
{ //創建一個畫布畫出剛剛創建的圖片
using (System.Drawing.Graphics g=System.Drawing.Graphics.FromImage(bitmip))
{
//隨即創建int類型的驗證碼
Random rand = new Random();
int code = rand.Next();
string strcode = code.ToString();
//把產生的驗證碼保存到session中
HttpContext.Current.Session["code"] = strcode;
// 設置畫布中的內容,字體大小,字體顏色
g.DrawString(strcode, new System.Drawing.Font("宋體", 12), System.Drawing.Brushes.Yellow, new System.Drawing.PointF(0, 0));//font是要回收的
//保存
bitmip.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
在aspx頁面調用session的值與用戶提交的比較。
新聞熱點
疑難解答