麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > .NET > 正文

Asp.net 基于Cookie簡易的權限判斷

2024-07-10 13:26:55
字體:
來源:轉載
供稿:網友
寫入Cookie頁面,創建cookie后,設置cookie屬性,并添加到Response.Cookies中讀取cookie,利用cookie的名字或索引從Request.Cookies中取得改寫Cookie,先創建一個同名的cookie,讀取Request中同名的cookie,把讀取cookie的屬性值付給新的對象,加入到Response.Cookies中創建一個BasePage頁面,其他的頁面繼承自這個頁面,把權限判斷的代碼有單個頁面的Page_Load轉移到BasePage的PreLoad中,下面是BasePage的主要代碼

復制代碼 代碼如下:


public class BasePage : System.Web.UI.Page
{
private string pageName;
public BasePage()
{
this.Page.PreLoad += Page_Load;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Uri r = this.Request.Url;
pageName = r.AbsolutePath;
if (NeedToCheck())
{
if (!HasAuthentication())
{
HttpContext.Current.Response.Redirect("NoAuthenticationPage.aspx");
}
}
}
}
private bool NeedToCheck()
{
if (pageName.Contains("NoAuthenticationPage.aspx") || pageName == "Login.aspx" )
{
return false;
}
return true;
}
private bool HasAuthentication()
{
//look into the config file or database,to see whether this page is in the allow accessing list of the role or not;
//the signature of the function is like this
//QueryInConfig(m_UserRole,pageName);
if (pageName.Contains("Default3.aspx") && UserRole == "2")
{
return false;
}
return true;
}
protected HttpCookie _RequestCookie;
protected HttpCookie _ResponseCookie;
private bool b_IsNewCookie = true;
public string UserRole
{
get
{
return GetCookieValue("UserRole");
}
set
{
SetCookieValue("UserRole", value);
}
}
public string UserName
{
get
{
return GetCookieValue("UserName");
}
set
{
SetCookieValue("UserName", value);
}
}
protected void SetCookieValue(string name, string value)
{
SetResponseCookie();
_ResponseCookie[name] = value;
}
private string GetCookieValue(string name)
{
SetReqeustCookie();
if (_RequestCookie != null)
{
return _RequestCookie[name];
}
return null;
}
protected void SetReqeustCookie()
{
_RequestCookie = HttpContext.Current.Request.Cookies["Cookie_Name"];
}
protected void SetResponseCookie()
{
if (b_IsNewCookie)
{
HttpContext.Current.Response.Cookies.Remove("Cookie_Name");
_ResponseCookie = new HttpCookie("Cookie_Name");
DateTime dtNow = DateTime.Now;
TimeSpan tsMinute = new TimeSpan(0, 2, 0, 0);
_ResponseCookie.Expires = dtNow + tsMinute;
_ResponseCookie["UserRole"] = UserRole;
_ResponseCookie["UserName"] = UserName;
HttpContext.Current.Response.Cookies.Add(_ResponseCookie);
b_IsNewCookie = false;
}
}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 热久久成人 | 精品一区在线视频 | 欧美成人精品一区二区 | 视频一区国产精品 | 久久精品视频2 | 国产成人精品免高潮在线观看 | 国产亚洲精彩视频 | 精品在线视频播放 | 国产精品视频一区二区三区四区国 | 免费a级片在线观看 | 中文字幕激情 | av电影免费观看 | 欧洲成人一区 | 激情视频导航 | 成人福利在线观看 | 国产精品一品二区三区四区18 | 欧美大穴 | 国产88久久久国产精品免费二区 | 久久国产91 | 一级一级一级一级毛片 | 成年免费视频黄网站在线观看 | 毛片视频网站 | 91在线免费观看 | 日韩欧美视频一区二区三区 | 亚洲国产高清一区 | 老女人碰碰在线碰碰视频 | 极色品影院 | 亚洲乱妇19p | 青青青在线免费 | 久久嗨 | 中日韩免费视频 | xnxx 美女19 | 免费在线性爱视频 | 精品亚洲午夜久久久久91 | 国产精品免费观在线 | 国产成视频在线观看 | 亚洲精品午夜视频 | 国产精品剧情一区二区在线观看 | 日韩黄色片免费看 | 美国av免费看 | 大学生一级毛片在线视频 |