一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - Asp.net 基于Cookie簡易的權限判斷

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

2019-07-03 17:24ASP.NET之家 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; 


延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 午夜dj影院在线观看完整版 | 亚洲第一成年免费网站 | 久久精品无码人妻无码AV蜜臀 | 免费看60分钟大片视频播放 | 亚洲国产一区 | 五月天中文在线 | 倩女还魂在线观看完整版免费 | 亚洲一区二区三区免费视频 | 亚洲精品在线看 | 国产精品视频二区不卡 | 欧美黑人性猛交╳xx╳动态图 | 欧美a级完整在线观看 | 成人欧美一区在线视频在线观看 | 日本免费精品视频 | 99精品热线在线观看免费视频 | 久久综合久综合久久鬼色 | 高h短篇辣肉各种姿势bl | 亚洲精品丝袜在线一区波多野结衣 | 日本免费高清在线观看播放 | 欧美日韩免费一区二区在线观看 | 美女毛片在线 | 黑人开嫩苞| 免费网站看v片在线香蕉 | 欧洲肥女大肥臀 | 国产在线观看色 | 99久久综合给久久精品 | 男人狂躁女人gif动态图 | 免费看又黄又爽又猛的视频软件- | 欧美成人aa久久狼窝动画 | 国内精品视频一区二区三区八戒 | 国产午夜一区二区在线观看 | 久久国产36精品色熟妇 | 娇妻与老头绿文小说系列 | 九九九九九九伊人 | 精品欧美一区二区三区久久久 | 青草国产 | 国产成人在线播放视频 | 波多野结衣中文字幕乱七八糟 | 免费国产成人高清视频网站 | 亚洲欧洲日产国码无码av | 99久久精品免费观看区一 |