Asp.net 基于Cookie簡易的權(quán)限判斷
更新時(shí)間:2010年01月13日 23:40:00 作者:
基于Cookie簡易的權(quán)限判斷代碼,需要的朋友可以參考下。
寫入Cookie頁面,創(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頁面,其他的頁面繼承自這個(gè)頁面,把權(quán)限判斷的代碼有單個(gè)頁面的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的簡單實(shí)例
- Asp.net cookie的處理流程深入分析
- asp.net關(guān)于Cookie跨域(域名)的問題
- 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)文章
ABP(現(xiàn)代ASP.NET樣板開發(fā)框架)系列之二、ABP入門教程詳解
ABP是為新的現(xiàn)代Web應(yīng)用程序使用最佳實(shí)踐和使用最流行工具的一個(gè)起點(diǎn)??勺鳛橐话阌猛镜膽?yīng)用程序的基礎(chǔ)框架或項(xiàng)目模板。接下來通過本文給大家詳細(xì)介紹ABP入門教程,感興趣的朋友一起看看吧2017-10-10.Net Core微信服務(wù)商二次進(jìn)件的開發(fā)
這篇文章主要介紹了.Net Core微信服務(wù)商二次進(jìn)件的開發(fā),包括服務(wù)商證書獲取方法及查詢進(jìn)件狀態(tài)的詳細(xì)代碼,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10ASP.NET中Web.config文件的層次關(guān)系詳細(xì)介紹
Web.config 是一個(gè)基于 XML 的配置文件,該文件的作用是對(duì)應(yīng)用程序進(jìn)行配置,下面為大家介紹下ASP.NET中Web.config文件的層次關(guān)系2014-01-01.NET?Core配置連接字符串和獲取數(shù)據(jù)庫上下文實(shí)例
這篇文章介紹了.NET?Core配置連接字符串和獲取數(shù)據(jù)庫上下文實(shí)例的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01asp.net下Linq To Sql注意事項(xiàng)小結(jié)
對(duì)于Linq 連接數(shù)據(jù)庫進(jìn)行操作時(shí)需注意的問題2008-10-10如何在ASP.NET Core中使用Session的示例代碼
這篇文章主要介紹了如何在ASP.NET Core中使用Session的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01保護(hù).net中的dll文件方法(防止破解、反編譯dll)
.net是一種建立在虛擬機(jī)上執(zhí)行的語言,它直接生成 MSIL 的中間語言,再由.net編譯器 JIT 解釋映象為本機(jī)代碼并交付CPU執(zhí)行。中間語言很容易被反編譯,所以研究下如何有效的保護(hù)dll文件2013-02-02