一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

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

寫入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;
}
}
}

AspNet技術Asp.net 基于Cookie簡易的權限判斷,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 免费播放一区二区三区 | 日本高清视频www | 欧美黑人巨大xxxxxfreexxxxx | 九色综合伊人久久富二代 | 亚洲成人综合在线 | 婷婷亚洲综合五月天小说在线 | 国产 麻豆 | 久久久噜噜噜久久中文字幕色伊伊 | 久久久久久亚洲精品中文字幕 | 国产精品永久免费视频观看 | 夜夜五月天 | 一级做a爰片久久毛片美女 一级做a爰片久久毛片人呢 | 99在线视频观看 | 欧美色视频网站 | 免费精品国产自产拍在 | 亚洲天堂成人在线观看 | 欧美成人综合视频 | 精品麻豆 | 国产日韩在线 | 91丨国产 | 日韩综合色 | 五月激情网站 | 精品久久网站 | 91精品福利一区二区 | 狠狠色噜噜狠狠狠狠2021天天 | 免费欧美一级 | 久久精品国产精品青草不卡 | 国产成人精品自拍 | 在线观看免费精品国产 | 欧美日韩国产亚洲一区二区 | 91视频免费网站 | 国产熟睡乱子伦视频观看看 | 国产永久在线视频 | 久久综合久久综合久久综合 | 国产精品亚洲视频 | 缴情啪啪三级小说网 | 国产在线观看精品一区二区三区91 | 精品国产一区二区三区免费看 | 久久精品99无色码中文字幕 | 亚洲性激情 | 在线免费观看色 |