前言: 經過前面三章的學習,相信我們隊asp.net已經有了一個初步的認識,下來我們就說說在開發網站中最常使用的Session,在這篇博文中我將采用理論加代碼的方法去學習他,相信如果寫過驗證碼的人就一定知道session了。下面就讓我們詳述吧!
- asp.net中使用session
(1) 先使用案例來簡單的說明一下session的讀取信息
1) 新建一個類文件,SessionMy.cs,寫入下面的代碼:
1 public class SessionMy 2 3 { 4 5 private static IDictionary<string, IDictionary<string, object>> data = new Dictionary<string, IDictionary<string, object>>(); 6 7 public IDictionary<string, object> GetSession(string SessionID) 8 9 { 10 11 if (data.ContainsKey(SessionID)) 12 13 { 14 15 return data[SessionID]; 16 17 } 18 19 else 20 21 { 22 23 IDictionary<string, object> session = new Dictionary<string, object>(); 24 25 data[SessionID] = session; 26 27 return session; 28 29 } 30 31 } }
2) 新建一個asp.net頁面,拖放兩個Button控件,第一個控件設置為“讀取Session”,第二個控件設置為“設置Session”,雙擊“設置Session“在其事件下面寫入如下代碼:
1 protected void Page_Load(object sender, EventArgs e) 2 3 { 4 5 if (Request.Cookies["MySessionID"] == null) 6 7 { 8 9 string sessionID = Guid.NewGuid().ToString(); 10 11 Response.SetCookie(new HttpCookie("MySessionID", sessionID)); 12 13 } 14 15 } 16 17 protected void Button2_Click(object sender, EventArgs e) 18 19 { 20 21 string sessionID = Request.Cookies["MYSessionID"].Value; 22 23 IDictionary<string, object> session = SessionMy.GetSession(sessionID); 24 25 session["name"] = "韓迎龍:"; 26 27 Session["服務器的數據"] = DateTime.Now.ToString(); 28 29 }
3) 然后雙擊”讀取Session”控件在其下面輸入如下代碼:
1 protected void Button1_Click(object sender, EventArgs e) 2 3 { 4 5 string sessionID = Request.Cookies["MySessionID"].Value; 6 7 IDictionary<string, object> session = SessionMy.GetSession(sessionID); 8 9 Button1.Text = Convert.ToString(session["name"]) + Convert.ToString(Session["服務器的數據"]); 10 11 }
(2) Cookie不能存儲過多信息,如果想要保存大量的數據,可以保存一個Guid到Cookie中,然后在服務器上面建立一個以Guid為key,復雜數據為value全局Dictionary,static字段對於不同用戶也另有一份,一次static實現多用戶共享數據。
(3) asp.net已經內置了一個Session機制,把上面的例子用asp.net重寫,不要放太多的對象到session,session會有超時銷毀的機制,發帖(服務器不可能知道瀏覽器是否開着,什么時候關閉)發帖記時,在線時間統計,考請求來判斷。
(4) 可以看到Session機制並不是Http協議規定的,是asp.net實現的,現在,php,jsp等大部分服務端技術都實現了session,原理都差不多。
(5) asp.net提供的session,新建一個web項目,在頁面中拖放兩個控件為設置和讀取,在單擊設置按鈕的事件下面寫入如下代碼:
1 protected void Button1_Click(object sender, EventArgs e) 2 3 { 4 5 Session["第一個值"] = DateTime.Now; 6 7 Session["第二個值"] = "&110"; 8 9 }
然后單擊讀取在其事件下寫入如下代碼:
1 protected void Button2_Click(object sender, EventArgs e) 2 3 { 4 5 Button2.Text = Convert.ToString(Session["第一個值"]) + Session["第二個值"]; 6 7 }
(6) Session版的自增,新建一個Web頁面,在頁面中拖放一個文本框和一個按鈕,文本框默認為0,然后雙擊Button控件在其時間下面寫如下代碼:
1 protected void Page_Load(object sender, EventArgs e) 2 3 { 4 5 //m每次服務器處理page_load都會執行,直接進入的時候才給Session里的value設置初始值 6 7 if (!IsPostBack) 8 9 { 10 11 Session["value"] = 0; 12 13 } 14 15 } 16 17 protected void Button1_Click(object sender, EventArgs e) 18 19 { 20 21 int v = Convert.ToInt32(Session["value"]); 22 23 v++; 24 25 Session["value"] = v; 26 27 TextBox1.Text = v.ToString(); 28 29 }
(7) Cookie是存在客戶端中的,Session是存在服務器端的,目的是一樣的,保存和當前客戶端相關的數據,不能放太大的數據,放的數據是object。
(8) 案例:用Session實現驗證碼
1) 新建兩個頁面,一個頁面是一般處理程序,一個頁面是驗證碼測試頁來實現驗證碼的操作,在一般處理程序中寫入如下代碼:
1 public class yanzhengmasession : IHttpHandler,System.Web.SessionState.IRequiresSessionState { 2 3 public void ProcessRequest(HttpContext context) 4 5 { 6 7 context.Response.ContentType = "image/JPEG"; 8 9 using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(100, 50)) 10 11 { 12 13 using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap)) 14 15 { 16 17 /* 18 19 g.DrawString("神州大地", new System.Drawing.Font("宋體", 20), System.Drawing.Brushes.Green, new System.Drawing.PointF(0, 0)); 20 21 g.DrawEllipse(System.Drawing.Pens.Red, new System.Drawing.Rectangle(10, 10, 10, 10)); 22 23 System.Drawing.Pen pen = (System.Drawing.Pen)System.Drawing.Pens.Red.Clone(); 24 25 pen.Width = 3; 26 27 g.DrawEllipse(pen, new System.Drawing.Rectangle(20, 20, 10, 10)); 28 29 bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); 30 31 */ 32 33 Random rand = new Random(); 34 35 int code = rand.Next(1000, 9999); //產生隨機驗證碼 36 37 string strCode = code.ToString(); 38 39 //如果需要在一般處理程序中操作session,必須實現IRequireSessionState接口,否則不顯示驗證碼 40 41 HttpContext.Current.Session["code"] = strCode; //將驗證碼放入Session 42 43 g.DrawString(strCode, new System.Drawing.Font("宋體", 30), System.Drawing.Brushes.Green, new System.Drawing.PointF(0, 0)); 44 45 bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); 46 47 } 48 49 } 50 51 } 52 53 }
2) 在驗證碼測試頁中寫入如下代碼,拖放一個TextBox控件和一個Button控件,然后給一個Div,代碼如下:
1 <div> 2 3 <img src="yanzhengmasession.ashx" onclick="this.src='yanzhengmasession.ashx?code='+new Date()+''" /> 4 5 <div>
在Buttot控件的事件下面寫入如下代碼:
1 protected void Button1_Click(object sender, EventArgs e) 2 3 { 4 5 string code = Convert.ToString(Session["code"]); 6 7 if (code == TextBox1.Text) 8 9 { 10 11 Response.Write("正確"); 12 13 } 14 15 else 16 17 { 18 19 Response.Write("錯誤"); 20 21 } 22 23 }
注:Session我們就說完了,下面博文我們開始說Http協議的一些知識點。我們的群號是:159227188,歡迎大家在這里交流asp.net的知識,我們可以學到很多東西的哦