asp.netcore mvc 權限攔截


1-背景介紹

  需要做一個簡單權限系統,基於 角色,用戶,菜單 的模式

 基於IActionFilter全局攔截,在內部跳轉或者瀏覽器跳轉的時候,攔截是成功的,當通過AJAX 請求的時候,頁面就不會跳轉

 

2-登錄后初始化該用戶權限到redis 緩存

  因為菜單沒有設置失效機制,所以登錄就刷新菜單緩存數據

 

3- 基於 IActionFilter 全局過濾

在OnActionExecuting 方法 用判斷 改動作是否有權限,沒有的話就跳轉一個 拒絕訪問的友好頁面。

首先,我們需要判斷是否是AJAX 請求,如果是的話,返回你的 正常的處理AJAX請求的返回JSON串 就可以了,前端就可以直接拿到然后做出正確的動作

如果不是則直接跳轉無權限 訪問 的頁面

 

4- 代碼及效果展示

 1 public void OnActionExecuting(ActionExecutingContext context)
 2         {
 3             var hasPermission = true;
 4             //權限攔截
 5             if (context.HttpContext.User.Identity.IsAuthenticated)
 6             {
 7                 var identity = context.HttpContext.User as ClaimsPrincipal;
 8                 var accountId = identity.Claims.First(x => x.Type == ClaimTypes.NameIdentifier).Value;
 9                 var accountName = identity.Claims.First(x => x.Type == ClaimTypes.Name).Value;
10                 if (accountName != "admin")
11                 {
12                     var menuDatalist = _menuMudoleStore.GetSysmodules(Convert.ToInt32(accountId), accountName, openRedis: true);
13                     var currentUrl = context.HttpContext.Request.Path.ToString().ToLower();
14                     if (currentUrl != "/Account/AccessDenied".ToLower())
15                     {
16                         if (menuDatalist == null && menuDatalist.Count <= 0)
17                         {
18                             hasPermission = false;
19                         }
20                         else
21                         {
22                             var mtypeid = (int)SysModuleType.module;
23                             var pageList = menuDatalist.Where(x => x.moduletypeid != mtypeid).ToList();
24                             if (!pageList.Any(x => x.url.ToLower() == currentUrl))
25                             {
26                                 hasPermission = false;
27                             }
28                         }
29                     }
30                 }
31             }
32             if (!hasPermission)
33             {
34                 if (context.HttpContext.Request.IsAjax())
35                 {
36                     context.Result = new JsonResult(new ReturnResult<string>
37                     {
38                         success = false,
39                         status = 302,
40                         message = "您無權限訪問",
41                         data= "/Account/AccessDenied"
42                     });
43                 }
44                 else
45                     context.HttpContext.Response.Redirect("/Account/AccessDenied");
46             }
47 }
后台代碼 GlobalActionFilter
 1 //初始化樹
 2         function initTree(roleid) {
 3             $.ajax({
 4                 url: '/Role/GetMenuTree',
 5                 type: 'get',
 6                 data: {
 7                     roleid: roleid
 8                 },
 9                 success: function (result) {
10                     console.log(result)
11                     if (result.success) {
12                         zTreeObj = $.fn.zTree.init($("#permissiontree"), setting, result.data);
13                         //
14                         toastr.info('數據加載成功', '提示');
15                     } else {
16                         toastr.error(result.message, '警告');
17                        //權限攔截
18                         if (result.status == 302) {
19                             $('#authperssionsformmodal').modal('hide');
20                             //setTimeout(function () {
21                             //    window.location.href = result.data;
22                             //}, 500);
23                         }
24                     }
25                     $('#dvloading').modal('hide');
26                 },
27                 beforeSend: function () {
28                     // Handle the beforeSend event
29                     $("#dvloading").modal({ backdrop: 'static', keyboard: false });
30                 },
31                 complete: function (xhr) {
32                     // Handle the complete event
33                     $('#dvloading').modal('hide');
34                     
35                 },
36                 error: function (e) {
37                     $('#dvloading').modal('hide');
38                     toastr.error('系統錯誤,請重試', '警告');
39                     window.clearInterval(timer);
40                 }
41             });
42         }
前端代碼

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM