国产精品一区二区三区……-大杳蕉伊人欧美一本遒在饯-日本不卡一区免费在线观看-国产亚洲欧美中文字幕

400-800-9385
網(wǎng)站建設(shè)資訊詳細(xì)

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

發(fā)表日期:2013-09-21 00:00:00   作者來源:方維網(wǎng)絡(luò)   瀏覽:6106   標(biāo)簽:ASP.NET    MVC3    

    ASP.NET本身擁有membership角色權(quán)限系統(tǒng),但是這個(gè)自帶的系統(tǒng)不夠靈活和強(qiáng)大,有時(shí)候操作起來比較繁瑣,這里方維網(wǎng)絡(luò)介紹一種如何根據(jù)控制器和方法自動(dòng)判定權(quán)限,簡(jiǎn)單方便。

    首先是定義一個(gè)類繼承ActionFilterAttribute類,然后重新方法OnActionExecuting  編寫如下代碼

    public override void OnActionExecuting(ActionExecutingContext filterContext)

        {
            if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                //沒有登錄執(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 = "沒有權(quá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)證不通過執(zhí)行的方法
                    }
 
                }
 
            }
          
        }


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

如沒特殊注明,文章均為方維網(wǎng)絡(luò)原創(chuàng),轉(zhuǎn)載請(qǐng)注明來自http://pdcharm.com/news/1993.html
安国市| 志丹县| 邻水| 安远县| 文水县| 望城县| 徐水县| 长寿区| 信阳市| 屯昌县| 张家川| 巴青县| 古蔺县| 白朗县| 溆浦县| 普陀区| 手机| 贺州市| 安乡县| 淄博市| 根河市| 文水县| 通海县| 金华市| 绥江县| 长沙市| 理塘县| 大田县| 韶关市| 开鲁县| 黄石市| 洛扎县| 九龙城区| 汉中市| 盱眙县| 韶山市| 温泉县| 图木舒克市| 涟水县| 海伦市| 鸡西市|