asp.net学习笔记·session基础
- 借助Cookie进行存取值;
- 值存在于服务器内存里面,在客户端进行修改没有影响,所以那是相当的安全;
- 所取值跟当前用户有关
模拟session类
public class SessionMoni { private static IDictionary<string,IDictionary<string,object>> data = new Dictionary<string,IDictionary<string,object>>(); public static IDictionary<string, object> GetSession(string sessionId) { if (data.ContainsKey(sessionId)) { return data[sessionId]; } else { IDictionary<string, object> session = new Dictionary<string, object>(); data[sessionId] = session; return session; } } } 设置取值小程序: if (null ==Request.Cookies["mySessionId"]) { string sessionId = Guid.NewGuid().ToString(); Response.SetCookie(new HttpCookie("mySessionId", sessionId)); } protected void Button2_Click(object sender, EventArgs e) { string sessionId = Request.Cookies["mySessionId"].Value; IDictionary<string, object> session = SessionMoni.GetSession(sessionId); this.Button2.Text = Convert.ToString(session["名字"]); } /// <summary> /// 取得session /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button3_Click(object sender, EventArgs e) { string sessionId = Request.Cookies["mySessionId"].Value; IDictionary<string, object> session = SessionMoni.GetSession(sessionId); session["名字"] = "许鸿飞"; }