asp.net BasePage類+Session通用使用者登入許可權控制

來源:互聯網
上載者:User
但是很多人都喜歡在

protected void Page_Load(object sender, EventArgs e) {}

裡面來寫代碼,甚至在某些按鈕裡面寫判斷session是否存在~~
這樣當然是能實現效果的,問題就在,如果有1000個頁面~~你需ctrl+C。。。Ctrl+V 很多次~~~
我的思路就是寫一個BasePage類繼承 System.Web.UI.Page

public class BasePage : System.Web.UI.Page { //pageunload事件,並不是指瀏覽器關閉,而是指頁面關閉,所以重新整理的時候,依然會執行以下事件 protected void Page_Unload(object sender, EventArgs e) { } protected override void OnPreInit(EventArgs e) { base.OnPreInit(e); if (!SessionData.IsLogin()) {//這裡寫 跳轉到登陸頁面:例如: Response.Redirect(string.Format("~/ReLogin.aspx?Page={0}", Request.Path)); }}

為什麼我這裡要帶 Page 參數,就是為了在登入成功以後可以返回到登入前的那一個頁面
另外我也貢獻一個SessionData類:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using ExpressPlatform.Common; namespace ExpressPlatform.Web.AppCode { public class SessionKey { public const string UserInfo = "user"; } /// <summary> /// 所有session中的資料,在該類管理 /// </summary> public class SessionData { /// <summary> /// 擷取session 中的 使用者資訊 /// </summary> /// <returns></returns> public static MdlSessionCustomerInfo GetUserInfo() { MdlSessionCustomerInfo userInfo = SessionManager<MdlSessionCustomerInfo>.GetSessionObject(SessionKey.UserInfo); if (userInfo == null) { userInfo = new MdlSessionCustomerInfo(); //把內容儲存到應用程式 SessionManager<MdlSessionCustomerInfo>.SetSessionObject(SessionKey.UserInfo, userInfo); } return userInfo; } /// <summary> /// 重新設定session 中的使用者資訊 /// </summary> /// <param name="userInfo"></param> public static void SetUserInfo(MdlSessionCustomerInfo userInfo) { SessionManager<MdlSessionCustomerInfo>.SetSessionObject(SessionKey.UserInfo, userInfo); } /// <summary> /// 清楚session中使用者資訊 /// </summary> public static void ClearUserInfo() { SessionManager<MdlSessionCustomerInfo>.SetSessionObject(SessionKey.UserInfo, null); } /// <summary> /// 是否登入 /// </summary> /// <returns></returns> public static bool IsLogin() { bool ret = false; MdlSessionCustomerInfo userInfo = SessionManager<MdlSessionCustomerInfo>.GetSessionObject(SessionKey.UserInfo); if (userInfo != null) ret = true; return ret; } } }
public class BasePage : System.Web.UI.Page
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.