發布時間:2020-08-06 21:44 作者:獨孤劍 閱讀:2060
using System; using System.Web; namespace WebApplication4 { /// <summary> /// ASP.NET Session 操作, 以用戶登錄退出為例, 實現Session的獲取, 設置, 清除 /// </summary> public partial class Default : System.Web.UI.Page { /// <summary> /// 載入, 獲取Session /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) { string user = ""; // 讀取Session if (HttpContext.Current.Session["User"] != null) { user= HttpContext.Current.Session["User"].ToString(); } Response.Write("當前登錄用戶: " + user); } /// <summary> /// 登錄, 設置Session /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnLogin_Click(object sender, EventArgs e) { // 設置Session, 5分鐘后過期 HttpContext.Current.Session["User"] = "Admin"; HttpContext.Current.Session.Timeout = 5; // 再次刷新頁面 HttpContext.Current.Response.Redirect("Default.aspx", false); } /// <summary> /// 退出, 清除Session /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnLogOff_Click(object sender, EventArgs e) { // 清除Session if (HttpContext.Current.Session != null) { HttpContext.Current.Session["User"] = null; } // 再次刷新頁面 HttpContext.Current.Response.Redirect("Default.aspx", false); } } }
微信打賞, 微信掃一掃
支付寶打賞, 支付寶掃一掃
如果文章對您有幫助,歡迎給作者打賞