欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.net BasePage類(lèi)+Session通用用戶(hù)登錄權(quán)限控制

 更新時(shí)間:2010年05月22日 13:52:33   作者:  
判斷用戶(hù)是否登錄,常用的方法就是通過(guò)Session來(lái)控制。
但是很多人都喜歡在
復(fù)制代碼 代碼如下:

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

里面來(lái)寫(xiě)代碼,甚至在某些按鈕里面寫(xiě)判斷session是否存在~~
這樣當(dāng)然是能實(shí)現(xiàn)效果的,問(wèn)題就在,如果有1000個(gè)頁(yè)面~~你需ctrl+C。。。Ctrl+V 很多次~~~
我的思路就是寫(xiě)一個(gè)BasePage類(lèi)繼承 System.Web.UI.Page
復(fù)制代碼 代碼如下:

public class BasePage : System.Web.UI.Page
{
//pageunload事件,并不是指瀏覽器關(guān)閉,而是指頁(yè)面關(guān)閉,所以刷新的時(shí)候,依然會(huì)執(zhí)行以下事件
protected void Page_Unload(object sender, EventArgs e)
{
}
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
if (!SessionData.IsLogin())
{//這里寫(xiě) 跳轉(zhuǎn)到登陸頁(yè)面:例如:
Response.Redirect(string.Format("~/ReLogin.aspx?Page={0}", Request.Path));
}}

為什么我這里要帶 Page 參數(shù),就是為了在登錄成功以后可以返回到登錄前的那一個(gè)頁(yè)面
另外我也貢獻(xiàn)一個(gè)SessionData類(lèi):
復(fù)制代碼 代碼如下:

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中的數(shù)據(jù),在該類(lèi)管理
/// </summary>
public class SessionData
{
/// <summary>
/// 獲取session 中的 用戶(hù)信息
/// </summary>
/// <returns></returns>
public static MdlSessionCustomerInfo GetUserInfo()
{
MdlSessionCustomerInfo userInfo = SessionManager<MdlSessionCustomerInfo>.GetSessionObject(SessionKey.UserInfo);
if (userInfo == null)
{
userInfo = new MdlSessionCustomerInfo();
//把內(nèi)容儲(chǔ)存到應(yīng)用程序
SessionManager<MdlSessionCustomerInfo>.SetSessionObject(SessionKey.UserInfo, userInfo);
}
return userInfo;
}
/// <summary>
/// 重新設(shè)置session 中的用戶(hù)信息
/// </summary>
/// <param name="userInfo"></param>
public static void SetUserInfo(MdlSessionCustomerInfo userInfo)
{
SessionManager<MdlSessionCustomerInfo>.SetSessionObject(SessionKey.UserInfo, userInfo);
}
/// <summary>
/// 清楚session中用戶(hù)信息
/// </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;
}
}
}

復(fù)制代碼 代碼如下:

public class BasePage : System.Web.UI.Page

相關(guān)文章

最新評(píng)論