Asp.net 基于Cookie簡(jiǎn)易的權(quán)限判斷
更新時(shí)間:2010年01月13日 23:40:00 作者:
基于Cookie簡(jiǎn)易的權(quán)限判斷代碼,需要的朋友可以參考下。
寫入Cookie頁(yè)面,創(chuàng)建cookie后,設(shè)置cookie屬性,并添加到Response.Cookies中讀取cookie,利用cookie的名字或索引從Request.Cookies中取得改寫Cookie,先創(chuàng)建一個(gè)同名的cookie,讀取Request中同名的cookie,把讀取cookie的屬性值付給新的對(duì)象,加入到Response.Cookies中創(chuàng)建一個(gè)BasePage頁(yè)面,其他的頁(yè)面繼承自這個(gè)頁(yè)面,把權(quán)限判斷的代碼有單個(gè)頁(yè)面的Page_Load轉(zhuǎn)移到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;
}
}
}
復(fù)制代碼 代碼如下:
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;
}
}
}
您可能感興趣的文章:
- asp.net利用cookie保存用戶密碼實(shí)現(xiàn)自動(dòng)登錄的方法
- asp.net各種cookie代碼和解析實(shí)例
- asp.net 操作cookie的簡(jiǎn)單實(shí)例
- Asp.net cookie的處理流程深入分析
- asp.net關(guān)于Cookie跨域(域名)的問(wèn)題
- asp.net中的cookie使用介紹
- asp.net下cookies操作完美代碼
- asp.net Cookie操作類
- ASP.NET Cookie 操作實(shí)現(xiàn)
- asp.net cookie的讀寫實(shí)例
- asp.net cookie清除的代碼
- ASP.NET登出系統(tǒng)并清除Cookie
相關(guān)文章
詳解.net core webapi 前后端開(kāi)發(fā)分離后的配置和部署
這篇文章主要介紹了.net core webapi 前后端開(kāi)發(fā)分離后的配置和部署,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
ASP.NET?Core?6.0?添加?JWT?認(rèn)證和授權(quán)功能
這篇文章主要介紹了ASP.NET?Core?6.0?添加?JWT?認(rèn)證和授權(quán),本文將分別介紹?Authentication(認(rèn)證)?和?Authorization(授權(quán)),通過(guò)實(shí)例代碼分別介紹了這兩個(gè)功能,需要的朋友可以參考下2022-04-04
mysql安裝后.net程序運(yùn)行出錯(cuò)的解決方法
這篇文章主要給大家介紹了關(guān)于mysql安裝后.net程序運(yùn)行出錯(cuò)的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-02-02
asp.net基于JWT的web api身份驗(yàn)證及跨域調(diào)用實(shí)踐
這篇文章主要介紹了asp.net基于JWT的web api身份驗(yàn)證及跨域調(diào)用實(shí)踐,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
設(shè)置DropDownList的當(dāng)前選項(xiàng)
2008-01-01
ASP.NET比較常用的26個(gè)性能優(yōu)化技巧
這篇文章主要給大家介紹asp.net中比較常用的26個(gè)性能優(yōu)化技巧,主要設(shè)計(jì)到asp.net中常用的26個(gè)性能優(yōu)化方面的內(nèi)容,對(duì)于asp.net中常用的26個(gè)性能優(yōu)化技巧感興趣的朋友可以參考下本篇文章2015-10-10
ASP.NET Core學(xué)習(xí)之使用JWT認(rèn)證授權(quán)詳解
這篇文章主要給大家介紹了關(guān)于ASP.NET Core學(xué)習(xí)之使用JWT認(rèn)證授權(quán)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用ASP.NET Core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

