ASP.NETMVC3如何實(shí)現(xiàn)自定義權(quán)限系統(tǒng)判定和實(shí)現(xiàn)

2023-03-12    分類: 網(wǎng)站建設(shè)

ASP.NET本身?yè)碛衜embership角色權(quán)限系統(tǒng),但是這個(gè)自帶的系統(tǒng)不夠靈活和強(qiáng)大,有時(shí)候操作起來(lái)比較繁瑣,這里創(chuàng)新互聯(lián)介紹一種如何根據(jù)控制器和方法自動(dòng)判定權(quán)限,簡(jiǎn)單方便。
首先是定義一個(gè)類繼承ActionFilterAttribute類,然后重新方法OnActionExecuting 編寫如下代碼

public override void OnActionExecuting(ActionExecutingContext filterContext)

{
if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
//沒(méi)有登錄執(zhí)行的操作
}
else
{
//判斷是否存在角色
FormsIdentity user = (FormsIdentity)HttpContext.Current.User.Identity;
var au = db.AdminUser.Where(a => a.username == user.Name).ToList();
if (au.Count > 0)
{
// string purview= au[0].group.purview;
bool is_authorize = true;
string error_msg = "沒(méi)有權(quán)限訪問(wèn)!";
string model = filterContext.RouteData.Values["controller"].ToString();
string action = filterContext.RouteData.Values["action"].ToString();
BLLAdminUser admin_user = new BLLAdminUser();
string purview=admin_user.getCheckPurview(model, action);//判斷權(quán)限
string mypurview = admin_user.getMyPurview();
is_authorize=admin_user.inPurview(purview, mypurview);
if (!is_authorize)
{
//如果驗(yàn)證不通過(guò)執(zhí)行的方法
}
}
}
}

然后在控制器或方法前面加上[Authorize]驗(yàn)證標(biāo)記就行了。

當(dāng)前名稱:ASP.NETMVC3如何實(shí)現(xiàn)自定義權(quán)限系統(tǒng)判定和實(shí)現(xiàn)
網(wǎng)頁(yè)路徑:http://www.muchs.cn/news24/243624.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁(yè)設(shè)計(jì)公司、用戶體驗(yàn)、靜態(tài)網(wǎng)站、做網(wǎng)站、網(wǎng)站收錄、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)